我目前正在尝试进行一些Selenium网络清理,但我一直遇到这个错误:
过时元素引用异常:消息:过时元素引用:元素未附加到页面文档
代码应该在http://www.grownjkids.gov/ParentsFamilies/ProviderSearch,连续单击结果的下一个按钮(">"),并在循环中从每页中抓取结果。它会在少数页面上正确地执行此操作,但偶尔会在随机页面上失败,但有上述例外。
我已经查看了许多有类似问题的StackOverflow帖子,并尝试了一些建议的修复方法,例如使用WebDriverWait类实现显式等待,在发生StaleElementReferenceException的条件下使用try/except块循环并使用driver.find_element…方法重新查找元素,以及同时尝试两种
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也很陌生。
我已经在你的代码中添加了流畅的等待元素,以供点击,请尝试以下代码:
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:NotBackedUpchromedriver.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异常时,循环将中断。
我希望它能帮助。。。
StaleElementReferenceException通常发生在您尝试与元素交互时,而不是在最初找到它时。
将您与元素的交互包装在Try-Except中,该Try-Exception会捕获StaleElementReferenceException。