当前位置: 首页 > 知识库问答 >
问题:

使用Selenium擦除时出现StaleElementReferenceException问题

夏涵畅
2023-03-14

我正在尝试完全加载此页面:https://candidat.pole-emploi.fr/offres/emploi/horticulteur/s1m1

我设置了一行代码来处理cookie弹出。

然后,我设置了一些行来单击加载更多结果按钮,以便加载完整的html,然后将其打印出来。

但单击一次后,我发现一条错误消息:

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

我不知道这意味着什么,也不知道如何修复它

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

site = 'https://candidat.pole-emploi.fr/offres/emploi/horticulteur/s1m1'
wd = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe", options=options)
wd.get(site)

time.sleep(10)

wait = WebDriverWait(wd, 10)

# click cookies popup
wd.find_element_by_xpath('//*[(@id = "description")]//*[contains(concat( " ", @class, " " ), concat( " ", "tc-open-privacy-center", " " ))]').click()

time.sleep(10)

# click show more button until no more results to load
while True:
    try:
        more_button = wait.until(EC.visibility_of_element_located((By.LINK_TEXT, 'AFFICHER LES 20 OFFRES SUIVANTES'))).click()
    except TimeoutException:
        break

time.sleep(10)

print(wd.page_source)
print("Complete")

time.sleep(10)
wd.quit()

共有3个答案

左丘涵畅
2023-03-14

尝试使用execute\u script方法,我认为这是解决此类问题最可靠的方法。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time

options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

site = 'https://candidat.pole-emploi.fr/offres/emploi/horticulteur/s1m1'
wd = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe", options=options)
wd.get(site)

time.sleep(10)

wait = WebDriverWait(wd, 10)

# click cookies popup
wd.find_element_by_xpath('//*[(@id = "description")]//*[contains(concat( " ", @class, " " ), concat( " ", "tc-open-privacy-center", " " ))]').click()

time.sleep(10)

# click show more button until no more results to load
while True:
    try:
        wait.until(EC.visibility_of_element_located((By.LINK_TEXT, 'AFFICHER LES 20 OFFRES SUIVANTES')))
        more_button = wd.find_element_by_link_text('AFFICHER LES 20 OFFRES SUIVANTES')
        wd.execute_script('arguments[0].click()', more_button)
        #print('clicked')
    except (TimeoutException, NoSuchElementException):
        break

time.sleep(10)

print(wd.page_source)
print("Complete")

time.sleep(10)
wd.quit()
吕向阳
2023-03-14

StaleElementReferenceException:stale元素引用:元素未附加到页面文档

指示对元素的引用现在已“过时”——该元素不再显示在页面的DOM上。出现这种预期的原因可能是您的DOM更新或刷新了。例如,在执行类似于单击的操作后,您的DOM可能会更新或刷新。此时,当您试图在DOM上查找元素时,您将遇到此错误。

您必须在更新或刷新的DOM中重新找到该元素

       try:  
            more_button = wait.until(EC.visibility_of_element_located((By.LINK_TEXT, 'AFFICHER LES 20 OFFRES SUIVANTES'))).click()  
     except StaleElementReferenceException:
            more_button = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.LINK_TEXT, 'AFFICHER LES 20 OFFRES SUIVANTES')))
            more_button.click()
支铭晨
2023-03-14

有许多方法可以处理陈旧的元素引用。

一个是像尝试重新单击web元素在一个while循环

您的link\u文本看起来也有误,请使用以下xpath:

# click cookies popup
driver.find_element_by_xpath('//*[(@id = "description")]//*[contains(concat( " ", @class, " " ), concat( " ", "tc-open-privacy-center", " " ))]').click()

time.sleep(10)

# click show more button until no more results to load
while True:
    try:
        more_button = wait.until(EC.visibility_of_element_located((By.XPATH, "//a[starts-with(@onclick,'tagDeClick') and contains(@href,'/offres/emploi.rechercheoffre:afficherplusderesultats')]")))
        ActionChains(driver).move_to_element(more_button).perform()
        attempts = 0
        while attempts < 2 :
            try:
                more_button.click()
                break
            except StaleElementReferenceException as exception:
                print(exception.msg)
            attempts = attempts  + 1

    except TimeoutException:
        break

time.sleep(10)

print(driver.page_source)
print("Complete")

time.sleep(10)

输出:

stale element reference: element is not attached to the page document
  (Session info: chrome=94.0.4606.81)

如果您在logs中看到这一点,并且您不希望看到这一点,则必须注释print(exception.msg)

进口:

from selenium.webdriver.common.action_chains import ActionChains
 类似资料:
  • 我正在尝试不同的方式选择一个特定的按钮使用seleninum webdriver与Java,但不幸的是,没有任何工作。 当我测试使用Selenium时,IDE是工作的。例如,我复制了相同的xpath,但当我试图在Java应用程序中进行测试时,任何东西都不起作用。我尝试使用不同的方法,通过.cssselector和通过.path。 这是我的HTML: 我需要选择带有文本“Create Applica

  • 问题内容: 运行测试时出现此错误:org.openqa.selenium.StaleElementReferenceException:元素不再附加到DOM 关于如何解决上述异常的任何想法?这发生在我的网格中,该网格具有动态的ref Xpath表达式 问题答案: 我遇到了同样的问题,找不到任何解决方案。提出了一个解决方案并将其发布在这里,希望这对遇到相同问题的人有所帮助。我创建了一个类来处理过时的

  • 问题内容: 我正在尝试在Chrome上使用Selenium 玩QWOP,但我一直收到以下错误: 在使用以下代码时: 相同的代码在Firefox上可以完美地工作,但是由于我想使用chrome的功能以无头模式运行webgl游戏,因此我无法真正切换到Firefox。 任何解决方法可以使它正常工作? 问题答案: NoSuchElementException selenium.common.exceptio

  • 问题内容: 我试图在使用selenium和phantomjs webdriver的linux服务器上运行python脚本;但是,我不断收到以下错误消息: 这是一个失败并生成此错误的简单测试脚本: 在路径上调用文件,返回: 有谁知道如何启动并运行该脚本?我已阅读了似乎类似的stackoverflow问题,并尝试应用建议的解决方案,例如通过npm重新安装幻像并使用sudo执行脚本,但是没有运气。如果我

  • 我使用以下代码按Id清除文本字段(在python 2.7中使用Selenium): StaleElementReferenceException Traceback(最近一次调用last)in()StaleElementReferenceException:消息:stale element reference:元素未附加到页面文档(会话信息:chrome=65.0.3325.181)(驱动程序信息

  • 我有一个数十万对象的列表。当每一个运行时,它都会根据给定的值执行一个可能很长的计算。正因为如此,我希望异步运行每个任务(最好是通过使用某种执行器),并在30秒后检索每次计算的结果,取消那些没有及时完成的结果。(所得值在其他地方使用。) 到目前为止,我就是这样实现它的: ArrayList存储每个要执行的,然后将其发送到ExecutorService以运行所有任务。我遇到的问题是,任务似乎是同步启动