【发布时间】: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