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

发布于 2021-01-29 15:03:07

import sys
import urllib2
import time
from bs4 import BeautifulSoup
from selenium import webdriver
import string
import re

reload(sys)
sys.setdefaultencoding('utf8')

baseUrl = 'https://www.breastsurgeons.org/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仅通过找到“

”第一次单击。之后它停止并给出以下错误。

   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)


selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
关注者
0
被浏览
63
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    错误说明了一切:

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

    for()循环内的程序中,您将在页面上找到<a>文本标记为 > >*的tag元素并进行调用,click()并且由于 HTML DOM
    发生更改而导致的click()事件。当您的程序第二次迭代循环时,也许未加载标识为的 WebElement ,但是 Selenium
    尝试引用以前已经 过时的 迭代中的搜索。因此,您会看到 StaleElementReferenceExceptionfor()

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

    ***

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

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

    可以诱导WebDriverWait在-结合expected_conditions子句设置为element_to_be_clickable
    WebElement 与特定 页码 (例如 345 ,等)是 可点击 如下:

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


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看