39

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.

davids
  • 6,259
  • 3
  • 29
  • 50

4 Answers4

62

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.

Pithikos
  • 18,827
  • 15
  • 113
  • 136
  • 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(). – user3326078 Jul 27 '18 at 22:03
  • The movement is relative to the center of the element, not the upper left corner. – Ken Shirriff Aug 24 '23 at 03: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.

Arran
  • 24,648
  • 6
  • 68
  • 78
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)
Pranjay Kaparuwan
  • 881
  • 1
  • 11
  • 17
  • 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 – William Baker Morrison Dec 26 '20 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.

Ewan
  • 14,592
  • 6
  • 48
  • 62