KDD Expert KDD Expert - 5 months ago 62
Python Question

Element is not longer attached to the DOM

My purpose is create list of elemnets and then click on it. For the first iteration loop works perfectly, it open first link. Then it failed.

My code is :

hrefList = driver.find_elements_by_xpath(".//a[contains(text(), 'כרטיס רופא')]")

print("length of List is : " + str(len(hrefList)))

for href in hrefList:

I get the next error message:

Message: The element reference of <a> stale: either the element is no longer attached to the DOM or the page has been refreshed

Answer Source

This error comes when element is not present in DOM. You have to come back to the main page and then click it

Try this code you will get a solution, This is java code

String URL="https://www.ida.org.il/?pageType=19&langId=1&paramIds=%2Con_321%2Con_322%2Con_354%2Con_355%2Con_320&scope=&parameterSearch=";
    WebDriver driver=new FirefoxDriver();
    List<WebElement> links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]"));
    System.out.println("Total links: "+links.size());
    for (int i=0;i<links.size();i++) {

             +"Current URL is: "+driver.getCurrentUrl());
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download