Selenium单击一次,但是下一次单击将返回StaleElementReferenceException

编程入门 行业动态 更新时间:2024-10-28 04:24:05
本文介绍了Selenium单击一次,但是下一次单击将返回StaleElementReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 import sys import urllib2 import time from bs4 import BeautifulSoup from selenium import webdriver import string import re reload(sys) sys.setdefaultencoding('utf8') baseUrl = 'www.breastsurgeons/new_layout/membership/membersearch/index.php' driver = webdriver.Chrome('/usr/local/Cellar/chromedriver/2.36/bin/chromedriver') driver.get(baseUrl) time.sleep(20) for p in range(1,282): driver.find_element_by_xpath("//a[contains(text(),'>>')]").click() time.sleep(2) driver.quit()

打开baseUrl后,我手动单击同意",然后搜索要显示的医生列表.我想翻阅清单.现在,Selenium仅通过找到">>"来第一次单击.之后它停止并给出以下错误.

After opening the baseUrl, I manually click agree and then search for a list of physicians to show up. I want to flip through the listing. Right now Selenium only clicks the first time by finding ">>". After which it stops and gives following error.

driver.find_element_by_xpath("//a[contains(text(),'>>')]").click() File "/Library/Python/2.7/site-packages/selenium-3.11.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 80, in click self._execute(Command.CLICK_ELEMENT) File "/Library/Python/2.7/site-packages/selenium-3.11.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 628, in _execute return self._parent.execute(command, params) File "/Library/Python/2.7/site-packages/selenium-3.11.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 312, in execute self.error_handler.check_response(response) File "/Library/Python/2.7/site-packages/selenium-3.11.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) seleniummon.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

推荐答案

错误说明了一切:

seleniummon.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

在程序中,您在for()循环内的页面上找到文本为>> 的<a>标签元素,并调用click(),由于click()事件, HTML DOM 更改.当您的程序第二次迭代for()循环时,也许未加载标识为driver.find_element_by_xpath("//a[contains(text(),'>>')]")的 WebElement ,但是 Selenium 尝试引用前一个搜索迭代已经过时的陈旧.因此,您会看到 StaleElementReferenceException .

In your program within the for() loop you are locating the <a> tag element with text as >> on a page and invoking click() and due to the click() event the HTML DOM changes. When your program iterates the for() loop for the second time perhaps the WebElement identified as driver.find_element_by_xpath("//a[contains(text(),'>>')]") doesn't gets loaded but Selenium tries to refer the search from the previous iteration which have already turned stale. Hence you see StaleElementReferenceException.

一种令人信服的页面迭代方式将是:

A convincing way to iterate the pages would be instead of :

driver.find_element_by_xpath("//a[contains(text(),'>>')]").click()

您可以诱导 WebDriverWait 与 expected_conditions 子句设置为 WebElement 和特定的,对于 WebElement 的webdriver_support/selenium.webdriver.support.expected_conditions.html#selenium.webdriver.support.expected_conditions.element_to_be_clickable"rel =" nofollow noreferrer> element_to_be_clickable 页号(例如, 3 , 4 , 5 等),可以点击,如下所示:

You can induce WebDriverWait in-conjunction with expected_conditions clause set to element_to_be_clickable for the WebElement with the particular Page Number (e.g. 3, 4, 5, etc) to be clickable as follows :

WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(),'3')]"))).click()

更多推荐

Selenium单击一次,但是下一次单击将返回StaleElementReferenceException

本文发布于:2023-10-12 21:20:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   Selenium   StaleElementReferenceException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!