【问题标题】:How to get all href values of <a> in selenium python如何在硒python中获取<a>的所有href值
【发布时间】:2021-02-18 05:43:23
【问题描述】:

您好,我正在使用 selenium,我想查找 webelement 中存在的所有 href 值

links=profiles.find_elements_by_tag_name("a").get_attribute('href')

但它给了我错误

AttributeError: 'list' object has no attribute 'get_attribute'

【问题讨论】:

    标签: python selenium-webdriver webdriverwait attributeerror getattribute


    【解决方案1】:

    您将获得a 标签列表,以便您循环访问并获取您的信息

    a_list = profiles.find_elements_by_tag_name("a")
    all_links = [item.get_attribute('href') for item in a_list]
    

    all_links 将拥有您需要的网址列表

    【讨论】:

      【解决方案2】:

      find_elements_by_tag_name(name) 会返回一个列表。其中get_attribute(name) 获取WebElement 的给定属性或属性。因此错误:

      AttributeError: 'list' object has no attribute 'get_attribute'
      

      解决方案

      您需要遍历元素并提取 href 属性的值,如下所示:

      print([my_elem.get_attribute("href") for my_elem in profiles.find_elements(By.TAG_NAME, "a")])
      

      理想情况下,您需要为visibility_of_all_elements_located() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

      print([my_elem.get_attribute("href") for my_elem in WebDriverWait(profiles, 20).until(EC.visibility_of_all_elements_located((By.TAG_NAME, "a")))])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 2023-01-25
        • 1970-01-01
        • 2010-10-14
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多