python - 无法修复 StaleElementReferenceException(未附加到文档的元素)
问题描述
我目前正在尝试做一些 Selenium 网络抓取,但我一直遇到这个错误:
StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档
该代码应该在http://www.grownjkids.gov/ParentsFamilies/ProviderSearch上不断单击结果的下一个按钮 (">") 并在循环中从每个页面中抓取结果。它会在几页上正确执行此操作,但会偶尔在随机页面上失败,但上述异常除外。
我已经查看了许多具有类似问题的 StackOverflow 帖子,并尝试了一些建议的修复,例如使用 WebDriverWait 类来实现显式等待,使用 try/except 块循环并使用 driver.find_element 重新查找元素...在发生 StaleElementReferenceException 的情况下使用方法,并尝试两者
driver.find_element_by_id
和
driver.find_element_by_xpath。
下面是我的代码:
url = "http://www.grownjkids.gov/ParentsFamilies/ProviderSearch"
driver = webdriver.Chrome('MY WEBDRIVER FILE PATH')
driver.implicitly_wait(10)
driver.get(url)
#clears text box
driver.find_element_by_class_name("form-control").clear()
#clicks on search button without putting in any parameters, getting all the results
search_button = driver.find_element_by_id("searchButton")
search_button.click()
#function to find next button
def find(driver):
try:
element = driver.find_element_by_class_name("next")
if element:
return element
except StaleElementReferenceException:
while (attempts < 100):
element = driver.find_element_by_class_name("next")
if element:
return element
attempts += 1
#keeps on clicking next button to fetch each group of 5 results
while True:
try:
nextButton = WebDriverWait(driver, 2000).until(find)
except NoSuchElementException:
break
nextButton.send_keys('\n')
table = driver.find_element_by_id("results")
html_source = table.get_attribute('innerHTML')
print html_source
我有预感将 WebDriverWait 增加到 2000 并且循环 100 次尝试并没有真正起作用(也许它不会进入那个块?)因为无论我增加多少结果都是一样的。对我的代码的任何反馈也很感激,因为这是我第一次使用 Selenium,而且我对 python 也很陌生。
解决方案
当 Web 驱动程序尝试对不再存在或无效的元素执行操作时,会发生 StaleElementReferenceException。
我已经在您的代码中添加了流畅的等待,以使元素可用于单击,请尝试以下代码:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException, WebDriverException, NoSuchElementException
from selenium.webdriver.common.by import By
driver= webdriver.Chrome('C:\NotBackedUp\chromedriver.exe')
url = "http://www.grownjkids.gov/ParentsFamilies/ProviderSearch"
driver.get(url)
#clears text box
driver.find_element_by_class_name("form-control").clear()
#clicks on search button without putting in any parameters, getting all the results
search_button = driver.find_element_by_id("searchButton")
search_button.click()
#keeps on clicking next button to fetch each group of 5 results
i=1
while True:
wait = WebDriverWait(driver, timeout=1000, poll_frequency=1, ignored_exceptions=[StaleElementReferenceException, WebDriverException]);
try:
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'next')))
element.click()
print("Clicked ===> ", i)
i+=1
except NoSuchElementException:
break
table = driver.find_element_by_id("results")
html_source = table.get_attribute('innerHTML')
print html_source
Fluent wait 将通过忽略 StaleElementReferenceException 和 WebDriverException 异常来尝试单击下一个符号。
当您收到 NoSuchElementException 异常时,循环将中断。
我希望它有帮助...
推荐阅读
- visual-studio-code - VSCode:悬停文档未显示完整的功能文档
- primefaces - 在哪里可以找到 Primefaces 3.4.1 捆绑包(开发包)?
- c++ - 从 MySQL 结果集中检索字符串列
- c# - 如何将firebase推送通知发送到ios应用程序
- swift - CoreData 一对多关系。NSSet 归属问题
- reactjs - 从静态分页到动态
- mysql - 在多个用户插入数据库的情况下,pymysql 的 lastrowid 函数是否正常工作?
- ruby-on-rails - 如何找到当前排队和工作的延迟作业数量?
- sql - 如何获取不在另一个表中的 sql 表中的列列表?
- python - 如果python中有零,如何将两个数组中的项目相乘?