Selenium2 Webdriver C# .Click() 列表 - 陈旧参考异常
我需要一些帮助,因为当我尝试解析要单击的标签列表时,我不断收到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是预期的行为。您离开了该元素所在的页面。当您向后导航时,这是一个新页面,并且该元素不再位于该页面上。
为了解决这个问题,如果可以的话,我建议绕过 Bys。假设您的锚链接都有唯一的 href,您可以生成如下列表(java 代码,但应翻译为 c#):
我不知道您页面的构成,所以我不知道是否可以生成 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#):
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.