I am running into this issue when trying to click a button using selenium. The button html reads as below:
<button class="Component-button-0-2-65 Component-button-d1-0-2-68">and 5 more</button>
My code is here:
button = EC.element_to_be_clickable((By.XPATH,'.//button[contains(@class,"Component-button-d")]'))
if button:
print("TRUE")
button.click()
My output is:
TRUE
Traceback (most recent call last):
File "", line 47, in <module>
button.click()
AttributeError: 'function' object has no attribute 'click'
I am stumped as to why 'button' element is found by selenium (print(True) statement is executed) but then the click() method returns an attribute error.
This is the page I am scraping data from: https://religiondatabase.org/browse/regions I am able to extract all the information I need on the page, so the code leading up to the click is working.
I was expecting the item to be clickable. I'm not sure how to troubleshoot the attribute error (function object has no attribute click). Because it I paste the xpath into the webpage, it highlights the correct element.
The element search is always done through the
driverobject which is the WebDriver instance in it's core form:But when you apply WebDriverWait, the
waitconfigurations are applied on thedriverobject.So even during WebDriverWait the
driverobject needs to be present along with the expected_conditions and the locator strategy.This usecase
This line of code:
button object references to a junk value but on probing returns
trueand prints TRUE but the click can't be performed asbuttonis not of WebElement type.Solution
Given the HTML:
To click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH with classname:
Using XPATH with classname and innerText:
Note: You have to add the following imports :