0

I'm having some problems with retrieving innerHTML of multiple elements in Python with Selenium.

When I retrieve the data from one element this works perfectly with the following code:

productid0 = driver.find_element_by_class_name("mod-article-tile__meta")
productid1 = productid0.get_attribute('innerHTML')

From the moment I change element to elementS then my code doesn't work anymore and I get the following error "AttributeError: 'list' object has no attribute 'get_attribute'":

productid0 = driver.find_elements_by_class_name("mod-article-tile__meta")
productid1 = productid0.get_attribute('innerHTML')

I need to use the elements as I want to get this from all my elements on a specific page.

Can anyone help me with this?

Thanks!

4 Answers 4

2

find_element_by_class_name returns an element, but find_elements_by_class_name returns a list of elements. Like the error suggests you are calling get_attribute() on a Python list, which is not a thing. You have to specify an element in that list:

products = driver.find_elements_by_class_name("mod-article-tile__meta")
# get the innerhtml of the first element in the list
innerhtml = products[0].get_attribute('innerHTML')
Sign up to request clarification or add additional context in comments.

Comments

1

What happens?

find_elements_by_class_name creates a list of all matches and that is why you could not access the innerHTML directly

How to fix that?

Loop over the list and get the innerHTML of each match:

for product in driver.find_elements_by_class_name("mod-article-tile__meta"):
    product.get_attribute('innerHTML')

Comments

0

Just loop through the productid0.

productid0 = driver.find_elements_by_class_name("mod-article-tile__meta")
for product in product0:
    print(product.get_attribute('innerHTML'))

Comments

0

The first part where you use find_element_by_class_name("mod-article-tile__meta")just returns you the first element with class name "mod-article-tile__meta" whereas find_elements_by_class_name("mod-article-tile__meta") returns you a list of elements with the class name "mod-article-tile__meta".

To use the elements you have to iterate through the list and call get_attribute on each member of the list.

productid0 = driver.find_elements_by_class_name("mod-article-tile__meta")
productid1 = list(map(lambda p: p.get_attribute('innerHTML') , productid0)) 

The above code is can be written as a for loop as well

productid0 = driver.find_elements_by_class_name("mod-article-tile__meta")
productid1 = []

for p0 in productid0:
    productid1.append(p0.get_attribute('innerHTML'))

Comments

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.