AttributeError: 'list' object has no attribute 'click' – Selenium Webdriver

Question:

I am trying to use click command in Selenium webdriver using python. But I am getting the below error. Can some one help me?

Traceback (most recent call last):
File "C:UsersvikramworkspaceLDCtest.py", line 13, in <module>
driver.find_elements_by_link_text("MISCQA Misc Tests").click()
AttributeError: 'list' object has no attribute 'click'

Here is my program

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.support.ui import WebDriverWait
import config

url = config.config.get('url')

driver = webdriver.Ie()
driver.get(url)

driver.find_elements_by_link_text("MISCQA Misc Tests").click()

driver.close()

I think I am missing some thing. Please suggest me

Asked By: vkrams

||

Answers:

The part of your code,

driver.find_elements_by_link_text("MISCQA Misc Tests")

is returning back a list and not an object of the selenium webdriver (what you want, object of the class having the function “click()” ) .

Most likely, the elements of the list would be the objects.

Print the list after that part of the code and check if the elements of the list are the ones that you need.

Answered By: user723556

maybe driver.find_elements_by_link_text("MISCQA Misc Tests")[0].click() or another index…

I don’t know Selenium, but I guess find_elements_by_link_text is finding more than one thing, or maybe the method always return a list rather than a single object. If you are absolutely sure that only one object should be the result of your search, than just use [0] as I mentioned, if you can’t assume that there will be only one object, than you need to come up with a stronger strategy

Answered By: Dan Niero

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

Answered By: vkrams

The statement driver.find_elements_by_link_text("MISCQA Misc Tests")
returns a list of WebElement some of which might not be clickable.

So you will have to loop through the list of WebElement’s returned and then click on those elements which are clickable.

You can check if a WebElement is clickable or not by using the isClickable() function.

I have not posted the code because I do not know Python. Hope this helps you.

Answered By: Hari Reddy

if you want single element so u can use:

driver.find_element_by_link_text("MISCQA Misc Tests")

or if you want whole list, then:

for x in self.driver.find_elements_by_link_text("MISCQA Misc Tests"):
    link = webdriver.ActionChains(self.driver).move_to_element(x).click(x).perform()
Answered By: priya

if the attribute of “MISCQA Misc Tests” only has one,You can try to change elements into element in this code ‘driver.find_elements_by_link_text(“MISCQA Misc Tests”)’
Hope the problem will be fix

Answered By: sherry

I found below solution I was using appiumrobotlibaray version 1.5 where

@{elemet}    get webelements     ${elemets}
click element  @{elemet}[1]

this code throw “AttributeError: ‘list’ object has no attribute ‘click’ error
downgrade appium library to previous version. 1.4.6
and this is working in my case.

Answered By: Keval Pansuriya

Refers to the locating elements documentation, here is an explanation of find_elements_*:

To find multiple elements (these methods will return a list)

So, to access particular element, using an index like this:

#first element
driver.find_elements_by_xpath("xpath")[0].click()

Or you can use a loop to access all elements in the list:

#assumed to click the checkbox
chks = driver.find_elements_by_xpath("xpath")
for chk in chks:
    chk.click()
Answered By: frianH
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.