[英]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')
只能应用于webdriver
或webelement
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.