简体   繁体   English

driver.find_elements_by_xpath('//div[@data-testid="tweet"]') 给出没有 Output

[英]driver.find_elements_by_xpath('//div[@data-testid="tweet"]') gives No Output

I am trying to scrape tweets using twitter scraper I have tried a lot of solutions but it gives out empty list.我正在尝试使用 twitter 刮板刮推推文我尝试了很多解决方案,但它给出了空列表。

driver.find_elements_by_xpath('//div[@data-testid="tweet"]')

Whole chunk:整块:

cards = driver.find_elements_by_xpath('//div[@data-testid="tweet"]')
if len(cards) > 0:
     card = cards[0]
else:
     raise NoSuchElementException('No cards were found')

error
NoSuchElementException                    Traceback (most recent call last)
/tmp/ipykernel_18368/2095008957.py in <module>
      3      card = cards[0]
      4 else:
----> 5      raise NoSuchElementException('No cards were found')

NoSuchElementException: Message: No cards were found

Screenshot截屏

(It's same as that in twitter scraper github repository: https://github.com/israel-dryer/Twitter-Scraper ) (与twitter刮板github仓库中的相同: https://github.com/israel-dryer/Twitter-Scraper

What happens?怎么了?

You try to select the tweets in element <div> but it is in an <article>您尝试 select 元素<div>中的推文,但它位于<article>

How to fix?怎么修?

Page need some time to load so wait until first tweet or somthing else is located first and then select more specific with article :页面需要一些时间来加载,所以等到第一条推文或其他东西首先被定位,然后 select 更具体的article

if WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//article[@data-testid="tweet"]'))):
    for e in driver.find_elements(By.XPATH, '//article[@data-testid="tweet"]'):
        print(e.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium driver.find_elements_by_xpath; 为什么没有返回数据 - Selenium driver.find_elements_by_xpath; why is there no data returned driver.find_elements_by_xpath 随机失败 - driver.find_elements_by_xpath randomly fails driver.find_elements_by_xpath 只搜索一个 class - driver.find_elements_by_xpath search on only one class 一次将多个“driver.find_elements_by_xpath”作为.text - Selenium - Python - Getting multiple 'driver.find_elements_by_xpath' as .text at once - Selenium - Python Selenium Python 通过 data-testid="menu" 查找跨度 - Selenium Python find span by data-testid="menu" 没有ID、名称等,driver.find_elements_by_xpath 不起作用怎么办? - What should I do when there is no ID, name, etc. and driver.find_elements_by_xpath won't work? 使用 Beautifulsoup 从 html 获取 data-testid 和属性 - Get data-testid and attributes from html using Beautifulsoup BeautifulSoup select_all 不适用于 data-testid 属性 - BeautifulSoup select_all does not work with data-testid attribute driver.find_elements() 没有给出预期的输出 - driver.find_elements() does not gives the expected output 当元素为 data-testid 时如何处理弹出“接受所有 cookie” - 在 Python 中使用 Selenium - How to handle the popup "Accepting all cookies" when the element is data-testid - Using Selenium in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM