I was wondering how to simulate a button click on a login page using Selenium. When I try the following, it gives me an error saying that the button is not clickable at the coordinates listed (635, 353).
JavaScript
x
3
1
browser = webdriver.Chrome()
2
browser.find_element(By.XPATH, "//*[@id='signin_btn']").click()
3
When I try using an ActionChain, it returns the Web Element (the button that’s supposed to be clicked) rather click.
JavaScript
1
3
1
action = ActionChains(browser)
2
action.click(on_element=browser.find_element(By.XPATH, "//*[@id='signin_btn']"))
3
Advertisement
Answer
I doubt that it returns a WebElement instance but rather the ActionChains instance.
This is due to the signature of the method and allows to chain some actions. Here’s the click
method:
JavaScript
1
19
19
1
def click(self, on_element=None):
2
"""
3
Clicks an element.
4
5
:Args:
6
- on_element: The element to click.
7
If None, clicks on current mouse position.
8
"""
9
if on_element:
10
self.move_to_element(on_element)
11
if self._driver.w3c:
12
self.w3c_actions.pointer_action.click()
13
self.w3c_actions.key_action.pause()
14
self.w3c_actions.key_action.pause()
15
else:
16
self._actions.append(lambda: self._driver.execute(
17
Command.CLICK, {'button': 0}))
18
return self
19
You can see that it return self
.
It won’t click until you call perform
.
action.click(on_element=el).perform()