AttributeError: 'list' object has no attribute 'click' - Selenium Webdriver
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
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
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
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()
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.