2017-09-25 99 views
-2

我的目的是创建elemnets列表,然后单击它。对于第一个迭代循环完美地工作,它打开第一个链接。然后失败了。元素不再连接到DOM

我的代码是:

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

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

    for href in hrefList: 
     print(href) 
     href.click() 

我得到一个错误信息:

selenium.common.exceptions.StaleElementReferenceException: 
Message: The element reference of <a> stale: either the element is no longer attached to the DOM or the page has been refreshed 
+0

你知道,如果你点击链接在新页面上会被加载,页面上的内容会发生变化,页面上可能不存在链接?情况如何? –

+0

@KDD U已尝试??????? – iamsankalp89

回答

1

此错误来当元素不存在于DOM。你必须回到主界面,然后点击

试试这个代码,你会得到一个解决方案,这是Java代码

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(); 
    driver.get(URL); 
    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++) { 


    links.get(i).click(); 
    System.out.println(i 
      +"Current URL is: "+driver.getCurrentUrl()); 
    driver.navigate().back(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    links=driver.findElements(By.xpath("//a[contains(@href, 'https://www.ida.org.il/?categoryId=96318&itemId')]")); 
    } 
+0

谢谢,请在勾上箭头时加注 – iamsankalp89