问题描述
嗨,我正在使用 selenium,我想找到 webelement 中存在的所有 hrefs 值
links=profiles.find_elements_by_tag_name("a").get_attribute('href')
但它给了我错误
AttributeError: 'list' object has no attribute 'get_attribute'
解决方法
您正在获取 a
标签列表,以便您可以循环获取您的信息
a_list = profiles.find_elements_by_tag_name("a")
all_links = [item.get_attribute('href') for item in a_list]
all_links
将包含您需要的网址列表
find_elements_by_tag_name(name) 会返回一个 list。其中 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")])
理想情况下,您需要为 WebDriverWait 引入 visibility_of_all_elements_located(),并且您可以使用以下任一 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")))])