1

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.

1
  • This error occurs because you're trying to use .find_element for a list. try to get data type of driver Commented Jan 27, 2023 at 13:40

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks you man! I am not sure the person who wrote the initial script deliberately did mistakes but anyway, your suggestion worked! I will ask few more doubts here in the comment section and it will be really great if you could help me with that as well. Thanks again!
The above code gets the first closed issue link all the time However I want to get links of all the closed issues in each page. - github.com/moodle/moodle/pulls?q=is%3Apr+is%3Aclosed Is it possible to do that? The array only stores 1 URL.
I'm sorry, I have to go now. Will be back tomorrow
Hi! I am still trying to get the links of all the closed issues in this page. Is it possible to do this?
I am going by page by page so just fixing the above code itself will be fine.
|
1

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.

1 Comment

The above code gets the first closed issue link all the time However I want to get links of all the closed issues in each page. - github.com/moodle/moodle/pulls?q=is%3Apr+is%3Aclosed Is it possible to do that? The array only stores 1 URL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.