【问题标题】:Why does Python selenium throw a StaleElementReferenceException when I try to access the text of an element? [duplicate]当我尝试访问元素的文本时,为什么 Python selenium 会抛出 StaleElementReferenceException? [复制]
【发布时间】:2020-10-30 03:08:29
【问题描述】:

我正在尝试使用 selenium 和 Python 访问元素的文本。我可以很好地访问元素本身,但是当我尝试获取文本时它不起作用。

这是我的代码:

from selenium import webdriver
driver = webdriver.Chrome() # I removed the path for my post, but there is one that works in my actual code
URL = "https://www.costco.com/laptops.html"
driver.get(URL)
prices = driver.find_elements_by_class_name("price")
print([price.text for price in prices])

如果我运行这段代码,我会得到:selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attach to the page document

但是,如果我要打印出元素本身,我没有问题。 我阅读了一些关于过时元素异常的先前帖子,但我不明白为什么它在这种情况下适用于我。为什么当我尝试访问文本时 DOM 会发生变化?为什么会这样?

【问题讨论】:

标签: python selenium selenium-chromedriver


【解决方案1】:

原来你只需要等待:

from selenium import webdriver
import time
driver = webdriver.Chrome() # I removed the path for my post, but there is one that works in my actual code
URL = "https://www.costco.com/laptops.html"
driver.get(URL)
time.sleep(3)

prices = driver.find_elements_by_class_name("price")

print([price.text for price in prices])

输出:

['$1,999.99', '$2,299.99', '', '', '$769.99', '', '$799.99', '$1,449.99', '$1,199.99', '$1,199.99', '$1,999.99', '$1,599.99', '$1,299.99', '$2,299.99', '$1,549.99', '$1,499.99', '$599.99', '$1,699.99', '$1,079.99', '$2,999.99', '$1,649.99', '$1,499.99', '$2,399.99', '$1,499.97', '$1,199.99', '$1,649.99', '$849.99', '']

正确的做法是使用WebDriverWaitSee


旧答案

我不完全确定为什么会这样。但我建议你试试BeautifulSoup:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Chrome() # I removed the path for my post, but there is one that works in my actual code
URL = "https://www.costco.com/laptops.html"
driver.get(URL)

soup = BeautifulSoup(driver.page_source)

divs = soup.find_all("div",{"class":"price"})

[div.text.replace("\t",'').replace("\n",'') for div in divs]

输出:

['$1,099.99',
 '$399.99',
 '$1,199.99',
 '$599.99',
 '$1,049.99',
 '$799.99',
 '$699.99',
 '$949.99',
 '$699.99',
 '$1,999.99',
 '$449.99',
 '$2,699.99',
 '$1,149.99',
 '$1,599.99',
 '$1,049.99',
 '$1,249.99',
 '$299.99',
 '$1,799.99',
 '$749.99',
 '$849.99',
 '$2,299.99',
 '$999.99',
 '$649.99',
 '$799.99']

【讨论】:

  • 你知道我们为什么需要等待吗?对不起,我不明白。
  • 网站加载需要时间。因此,如果 Selenium 尝试查找元素,那么当这种情况发生时,它将无法找到它们并给出此错误。
猜你喜欢
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 2019-11-19
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多