-1

Is there a difference between testing perspective and not usability, on clicking elements with selenium like this:driver.findElement(By.id("foobar")).click(); instead of using coordinates and the robot class.

Example: If an element is not clickable because of a bug, robot will not be able to click it. Will selenium be able to click it ?

luator
  • 4,769
  • 3
  • 30
  • 51
fx86f
  • 69
  • 1
  • 10
  • I don't understand the question. Are you asking that if you are not able to click on a button using coordinates in the Robot class(?), can you use Selenium to click it by element id instead? – Taegost Jun 28 '16 at 17:05
  • 1
    I was trying to understand what is closer(and why) to a simple users click, robot or selenium (or Js after all). – fx86f Jun 29 '16 at 00:45

4 Answers4

3

Selenium tries as much as possible to emulate what a regular user can and cannot do. When you use findElement(...).click() Selenium will raise an exception if operation cannot be performed. For instance, if an element exists in the DOM but is not displayed, an exception will be raised. And note that Selenium's .click() will try to scroll elements. If you ask it to click an element that is not visible but could be visible by scroll it, Selenium will scroll to bring it into view.

Also note that Selenium will usually take the element's coordinates and perform a click at those coordinates. So the idea that a moving element will always be hit by findElement(...).click() (expressed in this answer) is incorrect. Using findElement minimizes the window between which the coordinates are acquired and the click event is sent, but it does not completely eliminate it. The reason that Selenium works with coordinates is, again, to reproduce what a user would do. The user would see the element, move the mouse to the coordinates of the element, and click on it. If the element has an transparent overlay over it, then the overlay will get the click, rather than the element. This works because Selenium clicks at the coordinates rather than send a click directly to the element that you selected with findElement.

If you are using Robot to perform the click, the stakes are roughly the same as above, with a few caveats:

  1. Trying to click moving elements is more of an issue because the window of time between acquiring the coordinates and performing the click is greater.

  2. Robot does not know how the DOM is structured so won't scroll elements for you.

Rogério Peixoto pointed out that you can use JavaScript to perform the click. This will cause the event handler for click to be called for the element, irrespective of whether a user would be in fact able to access the element. This can allow clicking on elements that are not otherwise clickable but I would not do this unless there are overriding reasons to do so. I've gone over the difference between Selenium's click and the JavaScript click in this answer.

Community
  • 1
  • 1
Louis
  • 146,715
  • 28
  • 274
  • 320
1

If an element is not clickable because of a bug, robot will not be able to click it. Will selenium be able to click it?

It depends on what the bug is that is preventing the click. I would suggest that you use element.click() in almost all cases because it's more maintainable. For coordinate clicks, what if the element moves? Then you have to update your script with the new position where element.click() will just work. Another issue with coordinate clicking, what if your element moves and another one is in its place? That will likely cause your script to fail but will be very hard to track down. An example might be an unexpected popup that covers the element to be clicked. Now your coordinate click will hit the popup and eventually cause an error/failure. If you do element.click() you will at least get an error that another element would receive the click. You can then track down what is getting in the way from debugging.

JeffC
  • 22,180
  • 5
  • 32
  • 55
1

Selenium clicks in my experience click an element within a browser window, where as Robot click seem to click whatever is on the desktop at the time. Personally I would recommend you try to use Selenium clicks where possible as you can specify the window in which you are clicking.

N Grabham
  • 31
  • 9
0

Using this:

driver.findElement(By.id("foobar")).click();

Means that you will click the element with the foobar id regardless where it is.

If you use coordinates, you won't be able to click the element if they change their position.

Updating answer due to your updated question:

I'm not quite sure where you're trying to get with this.

In Selenium, if the element is visible it will receive the click event, if not you'll have an exception.

You could force click it with javascript:

WebElement element = driver.findElement(By.id("foobar"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

But it won't trigger the an eventual javascript event registered to that button, you would have to fire it manually.

Rogério Peixoto
  • 2,176
  • 2
  • 23
  • 31