Skip to content
Advertisement

ActionChain click() returning Web Element instead of clicking

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

browser = webdriver.Chrome()
browser.find_element(By.XPATH, "//*[@id='signin_btn']").click()

When I try using an ActionChain, it returns the Web Element (the button that’s supposed to be clicked) rather click.

action = ActionChains(browser)
action.click(on_element=browser.find_element(By.XPATH, "//*[@id='signin_btn']"))

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:

def click(self, on_element=None):
    """
    Clicks an element.

    :Args:
     - on_element: The element to click.
       If None, clicks on current mouse position.
    """
    if on_element:
        self.move_to_element(on_element)
    if self._driver.w3c:
        self.w3c_actions.pointer_action.click()
        self.w3c_actions.key_action.pause()
        self.w3c_actions.key_action.pause()
    else:
        self._actions.append(lambda: self._driver.execute(
                             Command.CLICK, {'button': 0}))
    return self 

You can see that it return self.

It won’t click until you call perform.

action.click(on_element=el).perform()

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement