python - StaleElementReferenceException:消息:过时的元素引用:使用 Selenium 和 Python 时,元素未附加到页面文档错误

标签 python selenium xpath css-selectors staleelementreferenceexception

我正在用 python 编写一个 selenium 程序来从网站获取链接,它第一次运行,但是当我重新运行它时,它会在浏览器中正确打开网站,然后出现错误

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

headers = {
    "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582",
    "Accept-Language": "fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3",
    "Connection": "keep-alive",
    "Accept-Encoding": "gzip, deflate, br",
}

edge_driver = './msedgedriver.exe'
s = Service( edge_driver )
browser = webdriver.Edge( service=s )

browser.get( "https://www.jumia.ug/always/" )

all_links = browser.find_elements( By.CLASS_NAME, "core" )
working_links = []
for l in all_links:
    if l.get_attribute( "href" ) is not None:
        working_links.append(l.get_attribute('href'))

print(working_links)

这是我每次运行后遇到的错误

C:\Users\eliHeist\PycharmProjects\webscraping\venv\Scripts\python.exe C:/Users/eliHeist/PycharmProjects/webscraping/getlinks.py
Traceback (most recent call last):
  File "C:/Users/eliHeist/PycharmProjects/webscraping/getlinks.py", line 34, in <module>
    if l.get_attribute( "href" ) is not None:
  File "C:\Users\eliHeist\PycharmProjects\webscraping\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 155, in get_attribute
    attribute_value = self.parent.execute_script(
  File "C:\Users\eliHeist\PycharmProjects\webscraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 878, in execute_script
    return self.execute(command, {
  File "C:\Users\eliHeist\PycharmProjects\webscraping\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\eliHeist\PycharmProjects\webscraping\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: MicrosoftEdge=96.0.1054.57)
Stacktrace:
Backtrace:
    Microsoft::Applications::Events::ILogConfiguration::ILogConfiguration [0x00007FF60587CB62+56946]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6054AA597+949863]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6054AD788+962648]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6054AE3EA+965818]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF605516D9F+1394287]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF605501546+1306134]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF605515D5D+1390125]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6055013BF+1305743]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6054D6AA9+1131385]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6054D7B8F+1135711]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF60556C2E5+1743797]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF60556A771+1736769]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF60574B519+2521]
    Microsoft::Applications::Events::EventProperty::to_string [0x00007FF6055420AE+1571198]
    Microsoft::Applications::Events::ILogConfiguration::ILogConfiguration [0x00007FF60587576C+27260]
    Microsoft::Applications::Events::ILogConfiguration::ILogConfiguration [0x00007FF6058750E4+25588]
    Microsoft::Applications::Events::ILogConfiguration::ILogConfiguration [0x00007FF605874F36+25158]
    Microsoft::Applications::Events::EventProperties::GetName [0x00007FF6057D02DC+211724]
    BaseThreadInitThunk [0x00007FFF89E254E0+16]
    RtlUserThreadStart [0x00007FFF8AF2485B+43]


Process finished with exit code 1

页面根本不刷新,我应该做什么或不做什么?

最佳答案

要打印 href 属性的值,您必须引入 WebDriverWait对于 visibility_of_all_elements_located()您可以使用以下任一 Locator Strategies :

  • 使用CSS_SELECTOR:

    driver.get("https://www.jumia.ug/always/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='newsletter_popup_close-cta']"))).click()
    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.core[data-brand='Always'][href]")))])
    
  • 使用XPATH:

    driver.get("https://www.jumia.ug/always/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='newsletter_popup_close-cta']"))).click()
    print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='core' and @data-brand='Always'][@href]")))])
    
  • 控制台输出:

    ['https://www.jumia.ug/always-maxi-long-7s-15736010.html', 'https://www.jumia.ug/always-roxanne-maxi-duo-long-16s-15736015.html', 'https://www.jumia.ug/always-ultra-long-s3-8s-15736020.html', 'https://www.jumia.ug/always-roxanne-dreamz-maxi-ex-long-8s-15736016.html', 'https://www.jumia.ug/stainless-steel-thermos-flask-3-litres-silver-always-mpg51110.html', 'https://www.jumia.ug/always-roxanne-dreamz-maxi-ex-long-16s-15736017.html', 'https://www.jumia.ug/always-ultra-new-12s-vp-sup-pr-african-15736026.html', 'https://www.jumia.ug/travel-vacuum-thermo-cup-450ml-red-always-mpg59272.html', 'https://www.jumia.ug/pressing-flask-3litres-silver-always-mpg49765.html', 'https://www.jumia.ug/stainless-steel-travel-mug-silver-always-mpg69987.html', 'https://www.jumia.ug/unbreakable-2.5-litres-vaccum-flask-silver-always-mpg54323.html', 'https://www.jumia.ug/stainless-steel-vacuum-flask-pressing-3.5l-silver-always-mpg56188.html', 'https://www.jumia.ug/always-dailies-flexistyle-slim-panty-liners-breathable-flexible-with-fresh-scent-pack-of-26-10968170.html', 'https://www.jumia.ug/stainless-steel-vacuum-flask-500ml-black-always-mpg48945.html', 'https://www.jumia.ug/always-flask-pressing-colour-silver-16418821.html', 'https://www.jumia.ug/vaccum-travel-flask-450ml-blue-always-mpg73899.html', 'https://www.jumia.ug/500mls-vacuum-hot-cold-bottle-flask-sliver-always-mpg54381.html', 'https://www.jumia.ug/always-ultra-lw-14s-vp-sup-pr-african-15736025.html', 'https://www.jumia.ug/pressing-2.0-litres-unbreakable-vaccum-jar-flask-stainless-steel-always-mpg56170.html', 'https://www.jumia.ug/stainless-steel-travel-cap-black-always-mpg65745.html', 'https://www.jumia.ug/always-stainless-steel-travel-mug-0.48l-navy-blue-8224366.html', 'https://www.jumia.ug/always-thermal-flask-cup-silver-black-20572534.html', 'https://www.jumia.ug/stainless-vacuum-travel-mug-450ml-black-always-mpg56189.html', 'https://www.jumia.ug/0.5l-insulated-stainless-steel-travel-mug-colour-varies-always-mpg73677.html', 'https://www.jumia.ug/stainless-steel-travel-mug-0.5l-gold-always-mpg67096.html', 'https://www.jumia.ug/ultra-platinum-long-7-pads-always-mpg59273.html', 'https://www.jumia.ug/life-travel-flask-450ml-red-always-mpg72907.html', 'https://www.jumia.ug/always-xtra-protection-feminine-panty-liners-extra-long-92-pieces-us-4766946.html', 'https://www.jumia.ug/always-zzz-overnight-pads-for-women-size-6-with-wings-for-worry-free-nights-20ct-14336082.html', 'https://www.jumia.ug/portable-vacuum-thermos-flask-bottle-0.5ltr-silver-always-mpg48676.html', 'https://www.jumia.ug/stainless-steel-vacuum-flask-700ml-silver-always-mpg59271.html', 'https://www.jumia.ug/always-vacuum-hot-cold-bottle-flask-500mls-always-mpg46382.html', 'https://www.jumia.ug/stainlesss-steel-side-pressing-vaccum-flask-3litres-silver-always-mpg65748.html', 'https://www.jumia.ug/always-stainless-steel-thermos-flask-jug-silver-always-mpg42506.html', 'https://www.jumia.ug/always-450ml-thermal-flask-maroon-5494832.html', 'https://www.jumia.ug/always-food-flask-18949481.html', 'https://www.jumia.ug/always-portable-vacuum-1.0ltr-thermos-flask-bottle-silver-8032566.html', 'https://www.jumia.ug/always-stainless-vaccum-flask-3-litre-silver-black-always-mpg46820.html', 'https://www.jumia.ug/stainless-steel-vaccum-flask-3.5l-silver-always-mpg48943.html', 'https://www.jumia.ug/always-stainless-steel-vacuum-flask-silver-5196899.html']
    
  • 注意:您必须添加以下导入:

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

关于python - StaleElementReferenceException:消息:过时的元素引用:使用 Selenium 和 Python 时,元素未附加到页面文档错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70542045/

相关文章:

XPath 和 nokogiri;教程/例子?

python - 基于fixture的自动pytest.mark修饰

python - PyPDF2<=1.19 存在 PDF 编码问题

python - 在 Python 中使用递归

将 sys.stdout 缓冲区设置为零的 Python 标准习惯用法不适用于 Unicode

xslt - 识别 XSLT 中的 null 元素,即使在子元素中也没有值

selenium - 是否可以使用 appium 和 nightwatchjs 为移动 native 应用程序编写测试?

python - Selenium Firefox Webdriver 抛出 URLError

selenium - PhantomJS 中的 webdriver 显示大小

python - Selenium 使用 XPath 从表格单元格 (td) 获取文本