Selenium VB : OpenQA.Selenium.StaleElementReferenceException: 过时的元素引用: 元素未附加到页面文档
Selenium VB : OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
我正在尝试使用 LinkText 单击一个元素。
例如:
myelement = driver.FindElement(By.LinkText(StoreFile)) 'Click on report by name
logger.Debug("Report Found as " & myelement.Text)
If myelement Is Nothing Then
GoTo endTry
Else
myelement.Click()
logger.Debug("Report clicked is " & StoreFile)
End If
但是,我得到以下 错误:
The Error Is OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=66.0.3359.139)
(Driver info: chromedriver=2.35.528161
(5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Click()
at ExcelAddIn1.Ribbon1.BrandReview(String ReadFile).
为什么即使元素在网页上可见,也无法使用 LinkText 找到该元素?有没有办法解决这个问题,请帮忙?
您可以试试这个代码:
int attempts = 0;
while(attempts < 2) {
try {
driver.FindElement(By.LinkText(StoreFile)).Click()
logger.Debug("Clicked on the link successfully")
break;
}
catch(StaleElementException e) {
}
attempts++;
}
我不熟悉 vb.net,但不得不使用不同语言的 Webdriver 处理 StaleElementReferenceExceptions。问题是引用不再有效,我怀疑尝试捕获它然后再次尝试它是否有效:引用仍然丢失,所以它可能会失败。
另外,我会尽可能避免使用重试机制,因为您希望测试在每次 运行 时都执行相同的操作。
我可能会在您每次需要时查找该元素,不要将其存储为变量,将 myelement.Click()
替换为
driver.FindElement(By.LinkText(StoreFile)).Click()
不清楚的是为什么要在它周围加上 if 语句。如果找不到该元素,则说明您没有单击它。测试的其余部分是否仍然有效?
我假设以下步骤取决于成功单击该元素。如果该元素不存在,那么测试应该会失败,对吗?
我正在尝试使用 LinkText 单击一个元素。 例如:
myelement = driver.FindElement(By.LinkText(StoreFile)) 'Click on report by name
logger.Debug("Report Found as " & myelement.Text)
If myelement Is Nothing Then
GoTo endTry
Else
myelement.Click()
logger.Debug("Report clicked is " & StoreFile)
End If
但是,我得到以下 错误:
The Error Is OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=66.0.3359.139)
(Driver info: chromedriver=2.35.528161
(5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.1.7601 SP1 x86_64)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Click()
at ExcelAddIn1.Ribbon1.BrandReview(String ReadFile).
为什么即使元素在网页上可见,也无法使用 LinkText 找到该元素?有没有办法解决这个问题,请帮忙?
您可以试试这个代码:
int attempts = 0;
while(attempts < 2) {
try {
driver.FindElement(By.LinkText(StoreFile)).Click()
logger.Debug("Clicked on the link successfully")
break;
}
catch(StaleElementException e) {
}
attempts++;
}
我不熟悉 vb.net,但不得不使用不同语言的 Webdriver 处理 StaleElementReferenceExceptions。问题是引用不再有效,我怀疑尝试捕获它然后再次尝试它是否有效:引用仍然丢失,所以它可能会失败。 另外,我会尽可能避免使用重试机制,因为您希望测试在每次 运行 时都执行相同的操作。
我可能会在您每次需要时查找该元素,不要将其存储为变量,将 myelement.Click()
替换为
driver.FindElement(By.LinkText(StoreFile)).Click()
不清楚的是为什么要在它周围加上 if 语句。如果找不到该元素,则说明您没有单击它。测试的其余部分是否仍然有效? 我假设以下步骤取决于成功单击该元素。如果该元素不存在,那么测试应该会失败,对吗?