【问题标题】:List comprehension with Selenium elements in Python not working with tweetsPython 中使用 Selenium 元素的列表理解不适用于推文
【发布时间】: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


【解决方案1】:

很大程度上取决于您如何尝试构建 tweets

理想情况下,要使用 Selenium 从所有 tweets 中提取文本,您必须为 visibility_of_all_elements_located() 诱导 WebDriverWait,您可以使用以下任一 @987654325 @:

  • 使用CSS_SELECTORget_attribute("innerHTML")

    print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "css_selector_of_tweets")))])
    
  • 使用XPATHtext属性:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "xpath_of_tweets")))])
    
  • 注意:您必须添加以下导入:

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

结尾

链接到有用的文档:

【讨论】:

    【解决方案2】:

    当您尝试列表理解时,元素的状态已更改。所以在列表理解之前获取推文元素,如下所示。

    tweets = driver.find_elements_by_xpath('YOUR_XPATH_HERE')
    tweets_lists = [tweet.text for tweet in tweets]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2020-05-07
      • 2022-01-10
      相关资源
      最近更新 更多