如何使用Webdriver Selenium C#执行每个标签表(tr)的onclick

问题描述 投票:0回答:1

我有一个带tr标签的onclick的html表。此“单击”将我发送到新的网页,然后我需要返回上一页并继续进行下一个“ tr单击”。enter image description here

直到现在,我已经实现了执行第一个onclick,然后当我返回时,Selenium给了我这个错误。

陈旧元素参考:元素未附加到页面文档中

我拥有的代码

            IWebElement table = driver.FindElement(By.Name("pg_table"));
            IReadOnlyCollection<IWebElement> rows = table.FindElements(By.XPath(".//tr[@onclick]"));
            IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            int rowCurrent = 0;
            foreach (IWebElement row in rows)
            {

                js.ExecuteScript($"arguments[{rowCurrent}].click();", row);
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("/html/body")));

                //retreive data from the new page....

                Thread.Sleep(1000);
                driver.Navigate().Back();
                ++rowCurrent;
           }

您能帮我吗?

提前感谢。

c# selenium navigation back
1个回答
0
投票

我用这个解决了这个问题,首先我在列表中得到了所有onclick javascript

List<string> linksPbds = new List<string>();

IWebElement table = driver.FindElement(By.Name("pg_table"));
IReadOnlyCollection<IWebElement> rows = table.FindElements(By.XPath(".//tr[@onclick]"));

foreach (IWebElement row in rows)
{
  linksPbds.Add(row.GetAttribute("onclick"));
}

然后我迭代列表的每个成员并执行javascript

foreach (var link in links)
{
    js.ExecuteScript(link);
    var airlineCode = driver.FindElement(By.XPath("/html/body/form/table[1]/tbody/tr[2]/td[1]")).Text;
    .... other html element
}

我不知道这是否是最好的方法,但这对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.