【发布时间】:2020-09-24 05:24:15
【问题描述】:
我正在使用 Selenium 抓取 Twitter 页面,我抓取的推文存储在列表变量 tweets 中。我可以正常遍历它们并使用以下方法从中提取文本:
for tweet in tweets:
print(tweet.text)
但是,当我尝试使用列表理解并这样做时
[tweet.text for tweet in tweets]
我收到了StaleElementReferenceException
StaleElementReferenceException: Message: The element reference of [object String] "b22c079f-684f-4d46-942b-d5dd69203728" is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
为什么会这样?
【问题讨论】:
-
[tweet.text for tweet in tweets]你在这个循环中做了什么动作。似乎您的 DOM 重新加载。查看描述不在当前框架上下文中,或文档已刷新 -
完全没有动作。 for 循环有效,列表推导无效。
-
@wrahool 您可能同时更新/刷新了浏览器内容
标签: python selenium selenium-webdriver webdriver webdriverwait