40

Using the Python version of Selenium, is it possible to click some element in the DOM and to specify the coordinates where you want to click it? The Java version has the method clickAt, which actually does exactly what I am looking for, but can't find the equivalent in Python.

4 Answers 4

63

This should do it! Namely you need to use action chains from webdriver. Once you have an instance of that, you simply register a bunch of actions and then call perform() to perform them.

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
el=driver.find_elements_by_xpath("//button[contains(string(), 'Lucky')]")[0]

action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(el, 5, 5)
action.click()
action.perform()

This will move the mouse 5 pixels down and 5 pixels right from the upper-left corner of the button I feel lucky. Then it will click().

Notice that you must use perform(). Else nothing will happen.

2
  • Bunch of actions followed by action.perform() will perform all the preceding actions. Alternatively you can append .perform() on each action so you won't forget which actions are not correctly performing eg: action.click.perform(). Commented Jul 27, 2018 at 22:03
  • The movement is relative to the center of the element, not the upper left corner. Commented Aug 24, 2023 at 3:03
7

The reason you are getting confused is clickAt is an old v1 (Selenium RC) method.

WebDriver has a slightly different concept, of 'Actions'.

Specifically, the 'Actions' builder for the Python bindings live here.

The idea of the clickAt command is to click at a certain position relative to a particular element.

The same is achievable within the WebDriver, using the 'Actions' builder.

Hopefully this updated documentation can help.

1
4

You can perform the task with the Action chains in the python specially with Edge browser:

from selenium.webdriver import ActionChains
actionChains = ActionChains(driver)
button_xpath  = '//xapth...' 
button = driver.find_element_by_xpath(button_xpath)
actionChains.move_to_element(button).click().perform()

But sometimes Action chain does not finds the DOM element. Hence better option to use execute scipt in following way:

button_xpath  = '//xapth...' 
button = driver.find_element_by_xpath(button_xpath)
driver.execute_script("arguments[0].click();", button)
1
  • 1
    Thanks for this! Any thoughts on why sometimes action chains don't find the DOM element? As I develop more with selenium it seems very common that the same method isn't universal Commented Dec 26, 2020 at 15:07
3

I've not personally used this method, but looking through the source code of selenium.py I've found the following methods that look like they'd do what you want - They look to wrap clickAt:

def click_at(self,locator,coordString):
    """
    Clicks on a link, button, checkbox or radio button. If the click action
    causes a new page to load (like a link usually does), call
    waitForPageToLoad.

    'locator' is an element locator
    'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse      event relative to the element returned by the locator.
    """
    self.do_command("clickAt", [locator,coordString,])


def double_click_at(self,locator,coordString):
    """
    Doubleclicks on a link, button, checkbox or radio button. If the action
    causes a new page to load (like a link usually does), call
    waitForPageToLoad.

    'locator' is an element locator
    'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse      event relative to the element returned by the locator.
    """
    self.do_command("doubleClickAt", [locator,coordString,])

They appear in the selenium object and here is their online API documentation.

3
  • Great!! Which class do they belong to?
    – davids
    Commented May 29, 2013 at 7:04
  • They're in the selenium object. I've actually just found their API documentation online - updating answer.
    – Ewan
    Commented May 29, 2013 at 7:08
  • Just one more question. How do you actually use this? I'm used to work with webdrivers objects, but never used this one
    – davids
    Commented May 29, 2013 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.