1

I am trying to click on an element using LinkText. Eg:

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

But, I get the following error:

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).

Is there a reason as to why it can't find the element using the LinkText even though the element is visible on the web page? Is there a solution to resolve this, please help?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
shettyrish
  • 99
  • 2
  • 8
  • Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium May 08 '18 at 06:39
  • This can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. – cruisepandey May 08 '18 at 06:56
  • 1
    As @DebanjanB stated this is StaleElementReference exception, You just try to "re-find" this element again, reference from this element is lost – Kovacic May 08 '18 at 07:43

2 Answers2

0

You can try this code :

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++;
      }
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

I'm not familiar with vb.net, but have had to deal with StaleElementReferenceExceptions with Webdriver in different languages. The problems is that the reference is not valid anymore, I doubt that try-catching it and then re-attempting it a second time will work: the reference is still lost so it will probably fail. Also, I would avoid using retry mechanisms as much as possible, as you want the test to perform the same actions every time you run it.

I would probably just look up the element every time you need it and don't store it as a variable, replace myelement.Click() with

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

What is not clear is why you have the if statement around it. If you can't find the element, you are not clicking it. Does the remaining part of your test then still work? I would assume that the following steps depend on successfully clicking the element. If the element is not there, then the test should fail, right?

Pieter A
  • 166
  • 3