【问题标题】:Can't fix StaleElementReferenceException (element not attached to document)无法修复 StaleElementReferenceException(未附加到文档的元素)
【发布时间】:2019-02-01 16:43:01
【问题描述】:

我目前正在尝试做一些 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 也很陌生。

【问题讨论】:

    标签: python selenium web-scraping selenium-chromedriver staleelementreferenceexception


    【解决方案1】:

    当 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 异常时循环会中断。

    希望对你有帮助……

    【讨论】:

    • 谢谢!让我试试看。
    • 是的!非常感谢。
    • 抱歉,stackoverflow 的新手。我投了赞成票,但由于我的声望点,它不会显示。
    【解决方案2】:

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

    将您与元素的交互包装在 Try except 中,它会捕获 StaleElementReferenceException。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 2021-12-19
      • 1970-01-01
      • 2019-09-30
      • 2022-01-21
      • 1970-01-01
      • 2012-12-17
      相关资源
      最近更新 更多