Clicking on coordinates without an identification element

As part of my selenium test for the login function, I would like to click a button, indicating its coordinates and instructing selenium to click on these coordinates. This would be done without actually defining the element itself (via id, xpath, etc.).

I understand that there are other more efficient ways to run the click command, but I'm looking for a concrete approach to this method to best suit the user experience. Thanks.

+19
source share
14 answers

Selenium will not allow you to do this.

-16
source

There is a way to do it. Using the ActionChains API, you can move the mouse over an element, adjust it using some offset (relative to the middle of the element), and then click in that place. Here's how to do it using webdriver in Python:

elem = find_element_by_selector(selector) ac = ActionChains(browser) ac.move_to_element(elem).move_by_offset(x_off, y_off).click().perform() 

I really want to fire the question. There are several reasons why you need to click in a specific place rather than on an element. In my case, I have an SVG diagram with an overlay element that captures all clicks. I want to simulate a click on one of the columns, but since there is overlap, Selenium cannot click on the element itself. This method will also be useful for image images.

+36
source

In C # API you use actions

 var element = driver.FindElement(By...); new Actions(driver).moveToElement(element).moveByOffset(dx, dy).click().perform(); 

Although it is better to use simple Id, CSS, Xpath selectors if possible. But functionality is where it is needed (i.e. clicking elements in specific geographic locations for functionality).

+17
source

This can be done using the Actions class in java

Use the following code -

 new Actions(driver).moveByOffset(x coordinate, y coordinate).click().build().perform(); 

Note. Selenium 3 does not support the Actions class for geckodriver

Also note that the x and y coordinates are relative values ​​from the current mouse position. Assuming the mouse coordinates are at (0,0) for starters, if you want to use absolute values, you can perform the following action immediately after clicking on it using the code above.

new Actions(driver).moveByOffset(-x coordinate, -y coordinate).perform();

+10
source

At first I used JavaScript code, it worked amazingly until the website clicked.

So, I found this solution:

First, import ActionChains for Python & Actively this:

 from selenium.webdriver.common.action_chains import ActionChains actions = ActionChains(driver) 

To click on a specific point in your sessions, use this:

 actions.move_by_offset(X coordinates, Y coordinates).click().perform() 

NOTE. The above code will only work if the mouse is not touching, use this to reset the mouse coordinates:

 actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), 0,0)) 

In full:

 actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), 0,0) actions.move_by_offset(X coordinates, Y coordinates).click().perform() 
+6
source

If using a commercial add-on for Selenium is an option for you, it’s possible: suppose your button is located at x=123, y=456 . Then you can use Helium to click an element in these coordinates as follows:

 from helium.api import * # Tell Helium about your WebDriver instance: set_driver(driver) click(Point(123, 456)) 

(I am one of the authors of Helium.)

+4
source

In Java, this worked for me when I clicked on coordinates regardless of any elements.

 Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(By.tagName("body")), 0, 0); actions.moveByOffset(xCoordinate, yCoordinate).click().build().perform(); 

The second line of code will return the cursor to the upper left angular view of the browser, and in the last line will click the x, y coordinates specified as a parameter.

+4
source

In Selenium Java, you can try it with Javascript:

 WebDriver driver = new ChromeDriver(); if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor) driver).executeScript("el = document.elementFromPoint(x-cordinate, y-cordinate); el.click();"); } 
+2
source

You may be able to do this through iMacros http://www.iopus.com/iMacros/

+1
source

I used a class of actions, like many of the above, but what I found useful was if I needed to find the relative position from the element that I used the add-on of Firefox add-on to get the relative coordinates. For instance:

  IWebDriver driver = new FirefoxDriver(); driver.Url = @"https://scm.commerceinterface.com/accounts/login/?next=/remittance_center/"; var target = driver.FindElement(By.Id("loginAsEU")); Actions builder = new Actions(driver); builder.MoveToElement(target , -375 , -436).Click().Build().Perform(); 

I got -375, -436 by clicking on an element and then dragging it back until I reached the point I needed. The coordinates that MeasureIT said I just read. In my example above, the only element I had on the page that was clickable was the loginAsEu link. So I started from there.

+1
source

Action chains can be a bit fictitious. You can also achieve this by running javascript.

 self.driver.execute_script('el = document.elementFromPoint(440, 120); el.click();') 
0
source
  WebElement button = driver.findElement(By.xpath("//button[@type='submit']")); int height = button.getSize().getHeight(); int width = button.getSize().getWidth(); Actions act = new Actions(driver); act.moveToElement(button).moveByOffset((width/2)+2,(height/2)+2).click(); 
0
source
 import pyautogui from selenium import webdriver driver = webdriver.Chrome(chrome_options=options) driver.maximize_window() #maximize the browser window driver.implicitly_wait(30) driver.get(url) height=driver.get_window_size()['height'] #get browser navigation panel height browser_navigation_panel_height = driver.execute_script('return window.outerHeight - window.innerHeight;') act_y=y%height scroll_Y=y/height #scroll down page until y_off is visible try: driver.execute_script("window.scrollTo(0, "+str(scroll_Y*height)+")") except Exception as e: print "Exception" #pyautogui used to generate click by passing x,y coordinates pyautogui.FAILSAFE=False pyautogui.moveTo(x,act_y+browser_navigation_panel_height) pyautogui.click(x,act_y+browser_navigation_panel_height,clicks=1,interval=0.0,button="left") 

This works for me. I hope you succeed guys :)

0
source

If you can see the source code of the page, always the best option is to turn to the button by its identifier or attribute NAME. For example, you have a login button that looks like this:

  <input type="submit" name="login" id="login" /> 

In this case, the best way to do

 selenium.click(id="login"); 

Just out of curiosity - isn't this basic HTTP authentication? In this case, maybe take a look at this: http://code.google.com/p/selenium/issues/detail?id=34

-3
source

Source: https://habr.com/ru/post/974009/


All Articles