1

我正在尝试使用 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找到元素是否有原因?有没有办法解决这个问题,请帮忙?

4

2 回答 2

0

我不熟悉 vb.net,但不得不用不同语言的 Webdriver 处理 StaleElementReferenceExceptions。问题是引用不再有效,我怀疑尝试捕获它然后再次尝试它会起作用:引用仍然丢失,所以它可能会失败。另外,我会尽可能避免使用重试机制,因为您希望测试在每次运行时都执行相同的操作。

我可能会在每次需要时查找元素而不将其存储为变量,替换myelement.Click()

driver.FindElement(By.LinkText(StoreFile)).Click()

不清楚的是为什么你有 if 语句围绕它。如果您找不到该元素,则说明您没有单击它。你的测试的其余部分是否仍然有效?我假设以下步骤取决于成功单击该元素。如果元素不存在,那么测试应该失败,对吧?

于 2018-05-08T08:46:46.087 回答
0

你可以试试这段代码:

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++;
      }
于 2018-05-08T07:00:29.240 回答