我正在嘗試完整加載此頁面: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()
uj5u.com熱心網友回復:
StaleElementReferenceException:過時的元素參考:元素未附加到頁面檔案
表示對元素的參考現在“過時”——該元素不再出現在頁面的 DOM 上。這種期望的原因可能是您DOM得到了更新或重繪 。例如,在執行類似click()您的操作后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()
uj5u.com熱心網友回復:
有很多方法可以處理過時的元素參考。
一種是嘗試在 while 回圈中重新單擊 web 元素。
您的 link_text 看起來也有誤,請使用以下 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
uj5u.com熱心網友回復:
嘗試使用 execute_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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324930.html
