我的目的是创建elemnets列表,然后单击它。对于第一次迭代循环工作完美,它打开第一个链接。然后失败了。
我的代码是:
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 :(得分:1)
当DOM中不存在元素时会出现此错误。你必须回到主页然后点击它
试试这段代码,你会得到一个解决方案,这是java代码
String URL="https://www.ida.org.il/?pageType=19&langId=1¶mIds=%2Con_321%2Con_322%2Con_354%2Con_355%2Con_320&scope=¶meterSearch=";
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')]"));
}