Selenium: Click coordinates by element offset does not apply the coordinates but clicks the element instead.

Created on 12 May 2017  路  18Comments  路  Source: SeleniumHQ/selenium

Meta -

OS:

Windows 8
Selenium Version:

3.41
Browser:
FF/IE

Browser Version:

FF 53.02 (32 bit)
geckodriver 0.16.1
Expected Behavior -

moveToElement(WebElement toElement, int xOffset, int yOffset)

Click on offset based on the element
Actual Behavior -

Clicked on the element instead.
Steps to reproduce -

Use default google page as test page
Try to click google apps --//a[contains(@title,'Google apps')]

moveToElement(WebElement toElement, int xOffset, int yOffset)

Actions builder = new Actions(driver);
builder.moveToElement(googleAPP, -50, 0).click().build().perform();

In FF, Google app is clicked - offset being ignored
In GC, Google image is clicked - offset being respected

Same code works of GC (GC (57.0.2987.133)/ChromeDriver 2.29) and FF before ( FF47.01/robotframework==3.0/selenium==2.53.6)

All 18 comments

You basically just raised the same issue I closed yesterday. We need a reproducible test case

Please provide concise reproducible test case so that we can act on this issue. Once this is done the issue will be re-opened. See CONTRIBUTING.md

I had added Steps to reproduce

Use default google page as test page
Try to click google apps --//a[contains(@title,'Google apps')]

moveToElement(WebElement toElement, int xOffset, int yOffset)

Actions builder = new Actions(driver);
builder.moveToElement(googleAPP, -50, 0).click().build().perform();

In FF, Google app is clicked - offset being ignored
In GC, Google image is clicked - offset being respected

No, you haven't. There is no code, no actual page URL

Use default google page as test page
Try to click google apps --//a[contains(@title,'Google apps')]

I have no idea what page this is.

moveToElement(WebElement toElement, int xOffset, int yOffset)

What element? You aren't indicating what element you're trying to select.

Actions builder = new Actions(driver);
builder.moveToElement(googleAPP, -50, 0).click().build().perform();

What is the expected outcome of this?

Use default google page as test page
Try to click google apps --//a[contains(@title,'Google apps')]

http://google.com

In FF, Google app is clicked - offset being ignored
In GC, Google image is clicked - offset being respected

Was this enough information?

I missed the selector in there, I'll give it a try

Can you try this?

builder.moveToElement(googleAPP, -50, 10).click().build().perform();

Since the element you're basing your movement on is larger than the one you want to click, it's trying to click above it if you use 0 for y. If you change it to 10 it works (at least for me). I'm assuming you're trying to click on the 'Images' link

The issue was in FF, the offset was not being respected. I tried builder.moveToElement(googleAPP, -50, 10).click().build().perform();
even builder.moveToElement(googleAPP, -500, 100).click().build().perform();
and seems like the element not the offset is being clicked.

Please see a better test page below
Credit goes to
https://github.com/Tattoo

<html>
  <head>
    <title>asdads</title>
    <style>
      #foo {
        width: 100px;
        height: 100px;
        border: 1px solid black;
      }
    </style>
    <script type="text/javascript">
      window.onload = function (){
        document.querySelector('#foo').onclick = function(ev){
          var ev = ev || window.event;
          var target = document.querySelector('#result');
          target.innerHTML = target.innerHTML + '<br />' + ev.clientX + ' ' + ev.clientY;
        }
      }
    </script>
  </head>
  <body >
    <div id="foo"></div>
    <div id="result"></div>
  </body>
</html>

Use the same code above to click css=#foo
Regardless the offset is given, printed coordinates on the page stay the same.

What I provided you didn't work? It worked for me. I had my own event watcher displaying coordinates in the browser console.

The new page you provided doesn't have all the HTML (no #foo element or #result)

Not working in Firefox but Chrome seem fine. The test HTML have to be put on a local web server.

Still working correctly for me. Can you provide your code? Here's a jsfiddle I created for it, the code, and images of the outcome:
https://jsfiddle.net/vyk8yc22/1/

WebDriver driver = new FirefoxDriver();
driver.get("https://jsfiddle.net/vyk8yc22/1/");
WebElement frame = driver.findElement(By.cssSelector("iframe[name='result']"));
driver.switchTo().frame(frame);
WebElement element = driver.findElement(By.cssSelector("#foo"));
Actions builder = new Actions(driver);
builder.moveToElement(element, -50, 20).click().build().perform();

With 10 y offset:
10 y-offset

With 20 y offset:
20 y-offset

Also, what selenium version are you using? 3.41 isn't a valid version. Are you running through standalone?

This issue happened after I updated to geckodriver 0.16.1

FF 53.0.2 (32-bit)/selenium==3.4.1/geckodriver 0.16.1
It was working on FF47.01/selenium==2.53.6
I was running python binary selenium2Library

You gave me java code, but you're using python? What version of geckodriver were you using before?

I'm not sure what else to try here. I've given you a completely reproducible scenario that works as intended. Unless you can provide the same that doesn't work, I'm going to have to close this.

geckodriver 0.16.1

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


driver = webdriver.Firefox()
driver.get("http://localhost:8000/test.html")
elem = driver.find_element_by_xpath("//div[@id='foo']")
#elem.click()
actions = ActionChains(driver)
actions.move_to_element(elem)
actions.click()
actions.move_by_offset(-10,0)
actions.click()
actions.move_by_offset(-100,100)
actions.click()
actions.perform()

All returns 59,59 in FF
image

Chrome works OK, although not throwing out bound exception as before
driver = webdriver.Chrome()
image

Using URL you setup

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
#driver = webdriver.Chrome()
driver.get("https://jsfiddle.net/vyk8yc22/1/")

f = driver.find_element_by_css_selector("iframe[name='result']")

driver.switch_to_frame(f)

elem = driver.find_element_by_xpath("//div[@id='foo']")
#elem.click()
actions = ActionChains(driver)
actions.move_to_element(elem)
actions.click()
actions.move_by_offset(-10,0)
actions.click()
actions.move_by_offset(-100,100)
actions.click()
actions.perform()

image

Alright, this appears to be a bug with the python bindings. The move_by_offset command is not implemented for w3c protocol which geckodriver uses. I'll get a fix in here shortly and it'll be in the next release. So much easier to figure things out when there's a reproducible test case with code.

Fixed in 58872c6

Was this page helpful?
0 / 5 - 0 ratings