Selenium WebDriver Error: AttributeError: 'list' object has no attribute 'click'

SeleniumAutomation TestingTesting Tools

We can get the Selenium webdriver error: AttributeError: 'list' object has no attribute 'click' while working on a test. Let us see an example of code where we have encountered such an error.

Example

Code Implementation

from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait
driver.implicitly_wait(0.5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify an element
m = driver.find_elements_by_name('search')
m.click()
#browser quit
driver.quit()

Output

In the above code, we have got the error as we have used find_elements_by_name instead of find_element_by_name to perform a click operation on a single element. The method find_elements_by_name returns a list of elements.

Here, we want to perform click operation on an element, so the webdriver fails to identify the element on which it should perform the click. In this scenario, if we want to use the find_elements_by_name method, we have to explicitly mention the index of the element to be clicked.

Example

Code Implementation

from selenium import webdriver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait
driver.implicitly_wait(0.5)
#url launch
driver.get("https://www.tutorialspoint.com/index.htm")
#identify an element with find_element_by_name
m = driver.find_element_by_name('search')
m.click()
m.send_keys('Selenium')
s = m.get_attribute('value')
print('Value entered: ' + s)
#browser quit
driver.quit()

Output

raja
Published on 29-Jun-2021 07:57:17
Advertisements