Selenium2 Webdriver C# .Click() 列表 - 陈旧参考异常

发布于 2024-12-05 11:55:22 字数 990 浏览 1 评论 0原文

我需要一些帮助,因为当我尝试解析要单击的标签列表时,我不断收到 StaleElementReference。

我所做的是在页面上迭代页面并生成一个对象 List<>与所有 a 标签

    private List<IWebElement> _pageLinks;
    public List<IWebElement> pageLinks
    {
        get
        {
            if (_pageLinks == null)
            {
                _pageLinks = InfoDriver.FindElements(By.TagName("a")).ToList();
            }
            return _pageLinks;
        }
    }

然后我想解析此列表,然后单击每个标签,然后返回到引用它的页面。

    private static SeleniumInformation si = new SeleniumInformation(ffDriver);

        si.pageLinks.ForEach(i =>
        {
            i.Click();
            System.Threading.Thread.Sleep(1000);
            ffDriver.Navigate().Back();
        });

发生的情况是,第一次单击后,它会转到新页面,然后返回到起始页面,但无法获取下一个链接。我尝试将其设置为静态元素,设置一个支持字段,以便它检查是否已有数据,但似乎单击后 IwebElement 会丢失列表,并且它也不会重建列表,所以我得到未处理 StaleElementReference 异常且在缓存中未找到元素。

这是 Selenium 中 IWebElement 类的错误还是我做错了什么?任何帮助将不胜感激。

I need some help because I keep getting a StaleElementReference when I try to parse a list of a tags to click.

What I have done is on page land I iterate through the page and generate an object List<> with with all the a tags

    private List<IWebElement> _pageLinks;
    public List<IWebElement> pageLinks
    {
        get
        {
            if (_pageLinks == null)
            {
                _pageLinks = InfoDriver.FindElements(By.TagName("a")).ToList();
            }
            return _pageLinks;
        }
    }

Then I want to parse this list, and click each one and then go back to the page it was referenced from.

    private static SeleniumInformation si = new SeleniumInformation(ffDriver);

        si.pageLinks.ForEach(i =>
        {
            i.Click();
            System.Threading.Thread.Sleep(1000);
            ffDriver.Navigate().Back();
        });

What happens is that after the first click it goes to the new page and then goes back to the starting page but it can't get the next link. I've tried setting it to a static element, setting a backing field so that it checks to see if there is data there already however it appears that on click the IwebElement looses the list and it doesn't rebuild the list either so I get a StaleElementReference exception not handled and element not found in cache.

Is this a bug in Selenium with the IWebElement class or am I doing something wrong? Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪之魂 2024-12-12 11:55:22

这是预期的行为。您离开了该元素所在的页面。当您向后导航时,这是一个新页面,并且该元素不再位于该页面上。

为了解决这个问题,如果可以的话,我建议绕过 Bys。假设您的锚链接都有唯一的 href,您可以生成如下列表(java 代码,但应翻译为 c#):

private static List<By> getLinks(WebDriver driver)
{
    List<By> anchorLinkBys = new ArrayList<By>();
    List<WebElement> elements = driver.findElements(By.tagName("a"));
    for(WebElement e : elements)
    {
        anchorLinkBys.add(By.cssSelector("a[href=\"" + e.getAttribute("href") + "\"]"));
        //could also use another attribute such as id.
    }
    return anchorLinkBys;
}

我不知道您页面的构成,所以我不知道是否可以生成 By 的动态地唯一标识您想要的元素。例如,如果所有元素都有相同的父元素,则可以使用 css 3 级选择器 nth-child(n)。希望您从上面的代码中得到一些想法。

This is the expected behavior. You left the page the element was on. When you navigated back, it is a new page and that element is no longer on it.

To work around this I would suggest passing around Bys instead, if you can. Assuming your anchorlinks all have unique hrefs, you could instead generate a list as follows (java code, but should translate to c#):

private static List<By> getLinks(WebDriver driver)
{
    List<By> anchorLinkBys = new ArrayList<By>();
    List<WebElement> elements = driver.findElements(By.tagName("a"));
    for(WebElement e : elements)
    {
        anchorLinkBys.add(By.cssSelector("a[href=\"" + e.getAttribute("href") + "\"]"));
        //could also use another attribute such as id.
    }
    return anchorLinkBys;
}

I don't know the makeup of your page so I don't know if it is possible to generate By's dynamically that uniquely identify the elements you want. For example if all the elements have the same parent, you could use the css level 3 selector nth-child(n). Hopefully you get some ideas from the above code.

内心旳酸楚 2024-12-12 11:55:22
    private void YourTest()
    {
        IWebDriver browserDriver = new FirefoxDriver();
        browserDriver.Navigate().GoToUrl(pageUrl);
        int linkCount= browserDriver.FindElements(By.TagName("a")).Count;

        for (int i = 0; i <= linkCount-1; i++ )
        {
            List<IWebElement> linksToClick = browserDriver.FindElements(By.TagName("a")).ToList();
            linksToClick[i].Click();
            System.Threading.Thread.Sleep(4000);
            if(some boolean check)
            {
              //Do something here for validation
            }
            browserDriver.Navigate().Back();
        }
        broswerDriver.Quit();
    }
    private void YourTest()
    {
        IWebDriver browserDriver = new FirefoxDriver();
        browserDriver.Navigate().GoToUrl(pageUrl);
        int linkCount= browserDriver.FindElements(By.TagName("a")).Count;

        for (int i = 0; i <= linkCount-1; i++ )
        {
            List<IWebElement> linksToClick = browserDriver.FindElements(By.TagName("a")).ToList();
            linksToClick[i].Click();
            System.Threading.Thread.Sleep(4000);
            if(some boolean check)
            {
              //Do something here for validation
            }
            browserDriver.Navigate().Back();
        }
        broswerDriver.Quit();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文