繁体   English   中英

Python Selenium Webdriver:如何使用get_attribute选择多个元素

[英]Python Selenium Webdriver: how to select more than one element using get_attribute

我正在尝试使用以下命令在给定页面上找到网址:

driver.find_element_by_css_selector('.listing-title a').get_attribute('href')

这足以抓取第一个网址。 但是,我不知道如何在页面上获取其他href属性。 我尝试将s添加到element

driver.find_elements_by_css_selector('.listing-title a').get_attribute('href')

但是我收到以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'get_attribute'

有什么建议吗?

我想通了, find_elements_by_css_selector返回一个列表,所以我需要遍历它:

x = driver.find_elements_by_css_selector('.listing-title a')
for each in x:
    print each.get_attribute('href')

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM