无法修复 StaleElementReferenceException(元素未附加到文档)

Can't fix StaleElementReferenceException (element not attached to document)

我目前正在尝试进行一些 Selenium 网络抓取,但我一直 运行 进入此错误:

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

代码应该在 http://www.grownjkids.gov/ParentsFamilies/ProviderSearch 上连续点击结果的下一个按钮 (">") 并循环从每个页面抓取结果。它会在几页上正确执行此操作,但偶尔会在出现上述异常的随机页面上失败。

我已经查看了许多具有类似问题的 Whosebug 帖子并尝试了一些建议的修复,例如使用 WebDriverWait class 实现显式等待,使用 try/except 块循环并在 StaleElementReferenceException 发生的情况下使用 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 也是相当陌生。

StaleElementReferenceException 通常在您尝试与元素交互时发生,而不是在您最初找到它时发生。

将您与元素的交互包装在 Try Except 中,而不是捕获 StaleElementReferenceException。

当 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 等待将通过忽略 StaleElementReferenceException 和 WebDriverException 异常来尝试单击下一个符号。

当您遇到 NoSuchElementException 异常时,循环将中断。

希望对您有所帮助...