如何避免使用集合在 C# 中使用Selenium"stale element exception"?



我有一个页面,里面有50个条目,我需要逐一导航到每个条目。异常是在我从第一个返回并尝试导航到第二个后抛出的。在谷歌上搜索了一段时间后,我发现这可能是因为主页的实例被删除了。我正在使用foreach循环,希望代码继续。代码如下:

IReadOnlyCollection<IWebElement> anchors = this.driverIpass.FindElements(By.CssSelector(".card-list-item.ng-scope"));
foreach (IWebElement anchor in anchors)
{
anchor.FindElement(By.XPath("//*[contains(@id,'workitem-goto-tablet-btn')]")).Click();
//below is test button to go back
driverIpass.FindElement(By.Name("editForm:j_idt693")).Click();
}

正如你所看到的,我在这里所要做的就是进入收藏中的每一件物品,然后返回。我对这一切都很陌生,有人能让我走上正确的道路吗?谢谢

试着保留一个id列表并在其中循环,而不是过时的anchors集合。

相关内容

最新更新