TypeError:WebElement不可迭代

时间:2017-05-16 13:21:20

标签: python selenium twitter

在我的Python脚本中,我试图抓取最多5个卷轴的Twitter页面,但我的脚本抛出TypeError表明WebElements不可迭代。任何解决这个问题的方法。

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

path_chromedriver = '/Users/SubrataMohanty/Desktop/chromedriver'
browser = webdriver.Chrome(path_chromedriver)
base_url = u'https://twitter.com/search?q='
query = u'%40sachin_rt'
url = base_url + query

browser.get(url)
time.sleep(1)

body = browser.find_element_by_tag_name('body')

for _ in range(5) :
    body.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)

tweets = browser.find_element_by_class_name('tweet-text')

for tweet in tweets :
    print(tweet.text)

错误:

abhijeet-mohanty-2:Desktop SubrataMohanty$ python tweet_scraper.py 
Traceback (most recent call last):
  File "tweet_scraper.py", line 23, in <module>
    for tweet in tweets :
TypeError: 'WebElement' object is not iterable

1 个答案:

答案 0 :(得分:1)

find_element_by_class_name()会返回单个元素。您打算按类名查找多个元素 - 使用find_elements_by_class_name()方法(请注意s):

tweets = browser.find_elements_by_class_name('tweet-text')
#                        HERE^
for tweet in tweets :
    print(tweet.text)