Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Click at certain position

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.

like image 631
davids Avatar asked May 29 '13 06:05

davids


People also ask

How can we click element on certain coordinates in Selenium?

We can use the ClickAt command in Selenium IDE. The ClickAt command has two arguments − the element locator and the coordinates which mentions the x and y coordinates of the mouse with respect to the element identified by the locator.

How can you verify the specific position of a web element?

There are two ways to verify the location of an element. getLocation() verifies the absolute location of an element. I don't recommend this, it can change all the time depending on loading speeds and the size of the window. getCssValue() can, to an extent, verify the relative location of an element.

What is getLocation method in Selenium?

The getLocation method can be executed on all the WebElements. This is used to get the relative position of an element where it is rendered on the web page. This position is calculated relative to the top-left corner of the web page of which the (x, y) coordinates are assumed to be (0, 0).

How do you right click and select Options in Selenium?

Selenium uses the Actions class to perform the right click action. The contextClick() is a method under Actions class to do the right click and once the menu opens, we can select an option from them via automation.


4 Answers

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.

like image 78
Pithikos Avatar answered Oct 18 '22 22:10

Pithikos


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.

like image 28
Arran Avatar answered Oct 18 '22 22:10

Arran


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)
like image 4
Pranjay Kaparuwan Avatar answered Oct 18 '22 23:10

Pranjay Kaparuwan


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.

like image 3
Ewan Avatar answered Oct 18 '22 21:10

Ewan