【问题标题】:Howto to execute onclick of each tag table (tr) with Webdriver Selenium C#如何使用 Webdriver Selenium C# 执行每个标记表(tr)的 onclick
【发布时间】:2020-05-29 00:35:13
【问题描述】:

我在 tr 标签上有一个带有 onclick 的 html 表格。这个“点击”将我带到一个新的网页,然后我需要返回上一页并继续下一个“tr click”。

到目前为止,我实现了执行第一个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】:

    我用这个解决了这个问题,首先我将所有的 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
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多