Home > Mobile >  AttributeError: 'list' object has no attribute 'find_element' - Selenium driver
AttributeError: 'list' object has no attribute 'find_element' - Selenium driver

Time:01-27

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. 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. But i am getting the AttributeError: 'list' object has no attribute 'find_element' error. Please help.

CodePudding user response:

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.
But driver.find_elements(By.XPATH,'//div[@aria-label="Issues"]') returns a list of web element objects. While find_element(By.CLASS_NAME,'h4') can be applied on webdriver or webelement object only, not on a list of objects.
That code can be refactored as mentioned by 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

CodePudding user response:

find_elements returns a list, and a list doesn't have a method find_element (as per your error message).

You could move the find_element method to 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.

  • Related