【问题标题】:Python Selenium Webdriver: how to select more than one element using get_attributePython Selenium Webdriver:如何使用 get_attribute 选择多个元素
【发布时间】:2015-12-17 13:53:21
【问题描述】:

我正在尝试使用以下内容在给定页面上查找网址:

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'

有什么建议吗?

【问题讨论】:

    标签: python selenium getattribute


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多