Python& Selenium:如何避免ratidend.com中的staleelementReementReferenceException错误

发布于 2025-01-26 23:14:40 字数 563 浏览 4 评论 0 原文

我想从与Python&硒。

但是在打印了某些公司名称后显示以下错误。

StaleElementReferenceException:消息:陈旧元素参考:页面文档未连接元素

如何避免此错误?

奇怪的是,在发生此错误之前,要打印一些公司名称。

如果您能给我一些暗示,这将不胜感激。

python

driver.get('https://www.dividend.com/dividend-champions/')
companies = driver.find_elements(by=By.CLASS_NAME, value="m-table-long-name")
print(len(companies)) #30
for company in companies:
  print(company.text)

I'd like to get company names from https://www.dividend.com/dividend-champions/ with Python & Selenium.

But the following error is displayed after some company names are printed.

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

How can I avoid this error?

Curiously enough, some company names are printed before occurrence of this error.

It would be appreciated if you could give me some hint.

Python

driver.get('https://www.dividend.com/dividend-champions/')
companies = driver.find_elements(by=By.CLASS_NAME, value="m-table-long-name")
print(len(companies)) #30
for company in companies:
  print(company.text)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2025-02-02 23:14:40

StaleElementReferenceException 表示所选元素可能不再在HTML DOM中。因此,您需要使用 WebDriverWait

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

from selenium.webdriver.chrome.options import Options

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


option = webdriver.ChromeOptions()
option.add_argument("start-maximized")

#chrome to stay open
option.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=option)
driver.get('https://www.dividend.com/dividend-champions/')


names = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//*[@class="mp-table-body"]/div[@class="mp-table-body-row-container mp-table-row t-static"]')))

for name in names:
    company = name.find_element(By.XPATH,'.//a[@class="m-table-body-link-text m-table-stocks-name-col m-table-long-name"]').text
    print(company)

输出:

Eversource Energy
Andersons Inc.
J.M. Smucker Co.
Cambridge Bancorp
Factset Research Systems Inc.
Graco Inc.
First Of Long Island Corp.
Stepan Co.
MDU Resources Group Inc
W. P. Carey Inc
Church & Dwight Co., Inc.
Lincoln Electric Holdings, Inc.
Matthews International Corp. - Ordinary Shares - Class A
New Jersey Resources Corporation
Bank OZK
Caterpillar Inc.
Brown & Brown, Inc.
Carrier Global Corp
Polaris Inc
International Business Machines Corp.
NextEra Energy Inc
Realty Income Corp.
RenaissanceRe Holdings Ltd
Cullen Frost Bankers Inc.
Otis Worldwide Corp
Roper Technologies Inc
West Pharmaceutical Services, Inc.
Bancfirst Corp.
Albemarle Corp.
Aptargroup Inc.

webdrivermanager

StaleElementReferenceException means selected element may no longer in html dom.However,there are plenty of reasons behind this exception like incorrect element selection,load time and so on. So You need to use webdriverwait.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

from selenium.webdriver.chrome.options import Options

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


option = webdriver.ChromeOptions()
option.add_argument("start-maximized")

#chrome to stay open
option.add_experimental_option("detach", True)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=option)
driver.get('https://www.dividend.com/dividend-champions/')


names = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//*[@class="mp-table-body"]/div[@class="mp-table-body-row-container mp-table-row t-static"]')))

for name in names:
    company = name.find_element(By.XPATH,'.//a[@class="m-table-body-link-text m-table-stocks-name-col m-table-long-name"]').text
    print(company)

Output:

Eversource Energy
Andersons Inc.
J.M. Smucker Co.
Cambridge Bancorp
Factset Research Systems Inc.
Graco Inc.
First Of Long Island Corp.
Stepan Co.
MDU Resources Group Inc
W. P. Carey Inc
Church & Dwight Co., Inc.
Lincoln Electric Holdings, Inc.
Matthews International Corp. - Ordinary Shares - Class A
New Jersey Resources Corporation
Bank OZK
Caterpillar Inc.
Brown & Brown, Inc.
Carrier Global Corp
Polaris Inc
International Business Machines Corp.
NextEra Energy Inc
Realty Income Corp.
RenaissanceRe Holdings Ltd
Cullen Frost Bankers Inc.
Otis Worldwide Corp
Roper Technologies Inc
West Pharmaceutical Services, Inc.
Bancfirst Corp.
Albemarle Corp.
Aptargroup Inc.

WebdriverManager

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文