28

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:\Users\vikram\workspace\LDC\test.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

1
  • driver.find_elements_by_link_text("MISCQA Misc Tests") returns list, not callable object Commented Jun 27, 2012 at 9:22

8 Answers 8

77

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

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

1 Comment

Why didn't you accept his answer then?
17

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

2 Comments

I used the above statement but no luck. do you think that I didn't import a correct class for it.
Vikram, I don't know this Selenium, but the error message you got clearly states that you got a LIST back, so you need to access the elements inside that list... What other error you get when you try to do so?
5

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()

Comments

2

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.

4 Comments

This is the output which I get [<selenium.webdriver.remote.webelement.WebElement object at 0x0000000002D9E630>]
WebDriver driver = new InternetExplorerDriver(); driver.get(url); WebElement element = driver.findElement(By.linkText("Miscellaneous Tests")); element.click(); - This is the code in Java. I want to simulate the same in Selenium Python
Yes. these are objects itself. So, just iterate over the list and call the click() method over them. For example, driver.find_elements_by_link_text("MISCQA Misc Tests")[0].click()
^I meant not an object of the selenium webdriver he expects. Edited to make it more precise. Thank you for pointing that out. :)
2

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()

Comments

1

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

Comments

0

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.

1 Comment

isClickable() returns boolean so it doesn't require. Anyways I found the answer
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.