2

I have a problem with select on href

<a id="1 - GovSearch" name="1 - GovSearch" title="Population and Immigration Authority" 
href="https://www.gov.il/en/Departments/population_and_immigration_authority" class="content-title 
first-result" ng-class="{ 'first-result': $first }">
 <!-- ngIf: item.Extension -->
 <span ng-class="item.SubTitle ? 'pipe-after':''" class="ng- 
     binding"> Population and Immigration Authority </span>
                                        <!-- ngIf: item.SubTitle -->
                                    </a>

I tried to make with linktext and getting error:

element.FindElement(By.LinkText("Population and Immigration Authority")).Click();

Getting this error:

OpenQA.Selenium.StaleElementReferenceException: 'stale element reference: element is not attached to the page document (Session info: chrome=83.0.4103.61)'
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

3

A bit unconclusive from the details why you would see a StaleElementReferenceException. However, the element is an Angular element and within the child <span> tag of the <a> element. So you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies as solutions:

  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[id$='GovSearch'][name$='GovSearch'][title='Population and Immigration Authority'] span.ng-binding"))).Click();
    
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@title='Population and Immigration Authority']//span[@class='ng-binding' and contains(., 'Population and Immigration Authority')]"))).Click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • i tried to ran but getting error with the xpth Message: Test method hw.UnitTest1.TestMethod1 threw exception: OpenQA.Selenium.WebDriverTimeoutException: Timed out after 20 seconds ---> OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@title='Population and Immigration Authority']//span[@class='ng-binding' and contains(., ' – Population and Immigration Authority')]"} (Session info: chrome=83.0.4103.61) Stack Trace: – danielshmu shmayovich May 27 '20 at 14:53
  • @danielshmushmayovich Did you try the _CssSelector_? However there seems to bean extra **–** within _contains(., ' – Population and Immigration Authority')_ – undetected Selenium May 27 '20 at 14:58
0

This error is usually caused when element has changed or removed because of something that user has tried to do. You can try catching exception and then click again

try {
    element.FindElement(By.LinkText("Population and Immigration Authority")).Click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
   element.FindElement(By.LinkText("Population and Immigration Authority")).Click();
}

Webdriver internally uses getElementAt method and a unique id called UUID to locate elements. Anytime this UUID changes for an element because of any reason and the call that user is making refers to old UUID, getElementAt method throws exception. You can also see exception referring to fxdriver.cache.getElementAt method. You can find official explanation of this error here

user1207289
  • 3,060
  • 6
  • 30
  • 66