[英]Python Selenium - 'WebElement'object is not iterable
I am trying to get the URLs of all the posts on the page so I can open them in another tab, can someone please help thx!我正在尝试获取页面上所有帖子的 URL,以便我可以在另一个选项卡中打开它们,有人可以帮忙吗?
def opening(self):
bot = self.bot
bot.maximize_window()
bot.get('https://www.instagram.com/explore/tags/'+hashtag+'/')
time.sleep(2)
def posts(self):
bot = self.bot
hrefs = bot.find_element_by_tag_name('a')
pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
pic_hrefs = [href for href in pic_hrefs if hashtag in href]
time.sleep(1)
print(hashtag +' pictures: ' + len(pic_hrefs))
I am a beginner so please don't make fun of me and thank you for everyone that helps!我是初学者,所以请不要取笑我,谢谢大家的帮助!
This line:这一行:
hrefs = bot.find_element_by_tag_name('a')
returns a selenium web element.返回一个硒网络元素。 It is not a list, or any other iterable element.
它不是列表或任何其他可迭代元素。 In other words, you won't be able to iterate through it with your for loop .
换句话说,您将无法使用for 循环遍历它。 Print it and you'll see what it is.
打印它,你会看到它是什么。 Use this line instead, which will return a list.
改用这一行,它将返回一个列表。 Note that I added an s .
请注意,我添加了一个s 。
hrefs = bot.find_elements_by_tag_name('a')
Also, did you make a typo in href
?另外,您是否在
href
中href
? Shouldn't it be hrefs
?不应该是
hrefs
吗? The last instance in there:那里的最后一个实例:
pic_hrefs = [href for href in pic_hrefs if hashtag in href]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.