list' object has no attribute 'get_attribute' while iterating through WebElements
Let us see what's happening in your code :
Without any visibility to the concerned HTML
it seems the following line returns two WebElements
in to the List
find_href
which are inturn are appended to the all_trails
List
:
find_href = browser.find_elements_by_xpath('//div[@class="text truncate trail-name"]/a[1]')
Hence when we print the List
all_trails
both the WebElements
are printed. Hence No Error.
As per the error snap shot you have provided, you are trying to invoke get_attribute("href")
method over a List
which is Not Supported. Hence you see the error :
'List' Object has no attribute 'get_attribute'
Solution :
To get the href
attribute, we have to iterate over the List
as follows :
find_href = browser.find_elements_by_xpath('//your_xpath')
for my_href in find_href:
print(my_href.get_attribute("href"))
Python Selenium problems with get_attribute('innerHTML')
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')
IndexError: list index out of range / AttributeError: 'list' object has no attribute 'get_attribute'
You are using a wrong locator.
This is why viewcth
is and empty list.find_elements_by_xpath(".//*[@class='eo2As ']//*[@class='EDfFK ygqzn']//*[@class='Nm9Fw']")
finds no matching elements and returns empty list.
So when you trying to get a first element from that list by viewcth[0]
you are getting
IndexError: list index out of range
.
If you trying to perform viewct = viewcth.get_attribute('innerHTML')
this gives you
AttributeError: 'list' object has no attribute 'get_attribute'
since viewcth
is a list. Empty, but still a list.
So you can not apply .get_attribute('innerHTML')
method on a list, it is not a web element.
If you want to get amount of like try this:
For images
likes = bdy.find_element_by_xpath(".//a[@class='zV_Nj']/span").text
For videos:
likes = bdy.find_element_by_xpath(".//div[@class='Nm9Fw']/a").text
Or
likes = bdy.find_element_by_xpath(".//div[@class='HbPOm _9Ytll']/span").text
Get the src value from a list of webelements with Selenium
I can find the mistake you have made.
Instead of this
images = [images.get_attribute('src') for img in images]
It should be
images = [img.get_attribute('src') for img in images]
Since you are iterating the list.
Now print the list you will get all src
values.
print(images)
AttributeError: 'list' object has no attribute 'click' - Selenium Webdriver
Thanks for helping out. I found the answer for myself. Idea given by "Dan Niero"
The problem is, I am using driver.find_element[s]
instead of driver.find_element
. So one s makes difference and calling a wrong method. In fact I am following the eclipse autocomplete :(. Obviously driver.find_elements_by_link_text
returns list so If I send click event it wont understand.
Thanks for helping and sorry for my bad question
-Vikram
Related Topics
Stopping a Thread After a Certain Amount of Time
How to Remove the Top and Right Axis in Matplotlib
How to Apply Itertools.Product to Elements of a List of Lists
Why Is the Borg Pattern Better Than the Singleton Pattern in Python
How to Check If All Items in a List Are There in Another List
Modular Multiplicative Inverse Function in Python
Running Jupyter via Command Line on Windows
Can Anyone Explain Python's Relative Imports
Uploading Multiple Files with Flask
Pandas Groupby Without Turning Grouped by Column into Index
How to Force a List to a Fixed Size
Adding Labels in X Y Scatter Plot with Seaborn
Pandas - Convert Strings to Time Without Date
Dataframe Set_Index Not Setting