简体   繁体   English

AttributeError: 'list' object 没有属性 'find_element' - Selenium 驱动程序

[英]AttributeError: 'list' object has no attribute 'find_element' - Selenium driver

I am in the process of rewriting this old pyton script ( https://github.com/muvvasandeep/BuGL/blob/master/Scripts/DataExtraction.py ) which used older version of Selenium. The aim of this script is to extract open and closed issues from open source projects form github. I am new to both python and Selenium. I am having hard time rewriting several things inside it.我正在重写这个旧的 pyton 脚本( https://github.com/muvvasandeep/BuGL/blob/master/Scripts/DataExtraction.py ),它使用旧版本的 Selenium。这个脚本的目的是提取打开和来自开源项目的已关闭问题形成 github。我是 python 和 Selenium 的新手。我很难重写其中的几件事。 Currently I am struggling to get this working:目前我正在努力让这个工作:

repo_closed_url = [link.get_attribute('href') for link in driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]').find_element(By.CLASS_NAME,'h4')]

the above should get all closed issues link from a github page and store it in the repo_closed_url array.上面应该从 github 页面获取所有已关闭的问题链接并将其存储在 repo_closed_url 数组中。 But i am getting the AttributeError: 'list' object has no attribute 'find_element' error.但我收到 AttributeError: 'list' object has no attribute 'find_element' 错误。 Please help.请帮忙。

find_elements returns a list, and a list doesn't have a method find_element (as per your error message). find_elements返回一个列表,并且列表没有方法find_element (根据您的错误消息)。

You could move the find_element method to link :您可以将find_element方法移动到link

repo_closed_url = [link.find_element(By.CLASS_NAME,'h4').get_attribute('href') for link in driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]')]

Which seems more of what you're trying to do, instead of iterating over a single element, you iterate over a list of elements from the find_elements method.这似乎更像是您尝试做的事情,而不是迭代单个元素,而是迭代find_elements方法中的元素列表。

I'm not sure this code line worked ever.我不确定这条代码行是否有效。
It uses driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]') method then trying to apply find_element(By.CLASS_NAME,'h4') on the output of previous method.它使用driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]')方法,然后尝试将find_element(By.CLASS_NAME,'h4')应用于先前方法的 output。
But driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]') returns a list of web element objects.但是driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]')返回一个包含 web 个元素对象的列表 While find_element(By.CLASS_NAME,'h4') can be applied on webdriver or webelement object only, not on a list of objects.find_element(By.CLASS_NAME,'h4')只能应用于webdriverwebelement object,不能应用于对象列表。
That code can be refactored as mentioned by dm2该代码可以按照 dm2 所述进行重构

repo_closed_url = [link.find_element(By.CLASS_NAME,'h4').get_attribute('href') for link in driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]')]

In this case driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]') output is a list of web element objects, you are performing inline iteration on this list with for link in (output of)(driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]')) and then appying .find_element(By.CLASS_NAME,'h4').get_attribute('href') on each link element in the previously received list of links在本例driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]') output 是一个包含 web 个元素对象的列表,您正在使用for link in (output of)(driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]'))然后.find_element(By.CLASS_NAME,'h4').get_attribute('href')先前收到的链接列表中的每个link元素

暂无
暂无

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

相关问题 AttributeError:模块“selenium.webdriver”没有属性“find_element” - AttributeError: module 'selenium.webdriver' has no attribute 'find_element' AttributeError: 'str' object 没有属性 'find_element' - AttributeError: 'str' object has no attribute 'find_element' AttributeError: 'list' object 在尝试爬取 metro.co.kr 时没有属性 'find_element' - AttributeError: 'list' object has no attribute 'find_element' while trying to crawl subway.co.kr Expected_conditions._find_element引发错误:AttributeError:'str'对象没有属性'find_element' - expected_conditions._find_element throws an error: AttributeError: 'str' object has no attribute 'find_element' Selenium AttributeError:list对象没有属性find_element_by_xpath - Selenium AttributeError: list object has no attribute find_element_by_xpath Selenium Chromedriver:当禁用 w3c 时,'dict' 对象没有 find_element() 方法的属性 'click' - Selenium Chromedriver : 'dict' object has no attribute 'click' for find_element() method when w3c is disabled Selenium-AttributeError:对象没有属性'find_element_by_css_selector' - Selenium - AttributeError: object has no attribute 'find_element_by_css_selector' Web 抓取 Selenium 错误:“with AttributeError: 'list' object 没有属性 'find_element_by_class_name'” - Web scraping Selenium Error: "with AttributeError: 'list' object has no attribute 'find_element_by_class_name' " Selenium AttributeError: 'list' object 没有属性 'text' - Selenium AttributeError: 'list' object has no attribute 'text' python Selenium 查找元素 - python Selenium find_element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM