0

This issue is due to the chrome driver always clicks the middle of the element in attempt to be faithful to what an actual user does. So i was thinking of this approach:

First instead of locate an element and click:

driver.fineElement(By.xpath("bla bla")).click()

Write generic function that click on WebElement:

def clickOnWebElement(WebElement webElement) {
 int counter = 0;
 boolean isClicked = false;

 Thread.sleep(1000);
try {
    while (count < 2 && !isClicked) {

     if (count == 0) {
        webElement.click()
        isClicked = true;
     }     
     else if (count == 1) {
        Actions action = new Actions(driver);
        action.moveToElement(webElement).click().perform();
        isClicked = true;
       }
     else if (count == 2) {
        JavascriptExecutor js =(JavascriptExecutor)driver;
       js.executeScript("window.scrollTo(0,"element.getLocation().x+")");
        webElement.click();
        isClicked = true;
       }
    }
  }
catch(Exception ex) {
    count++;
    Thread.sleep(2000);
  }
}

And then when this exception occurs try different way to click.

You think this approach can work ?

david hol
  • 1,272
  • 7
  • 22
  • 44
  • i think it can work but what happens suppose if every else if is executed but still element in question has not fixed position try to add Thread.sleep in every else if may be that helps – Rajnish Kumar May 23 '16 at 12:52

1 Answers1

1

Please go through this stack overflow answer to have a better understanding.

UPDATE Also we can try

There are many Conditions that we can use withing Webdriver tests.

1. visibilityOf(WebElement element) : An expectation for checking that an element, known 
to be present on the DOM of a page, is visible.
2. visibilityOfElementLocated(By locator) : An expectation for checking that an element 
is present on the DOM of a page and visible.

In the above two conditions we are waiting for an element to be present on the DOM 
of a page and also visible. These works fine only when the element is loaded completely.

Also please try like below

Try to click using Y coordinates

WebElement elementToClick = driver.findElement(By.xpath("Your xpath"));
// Scroll the browser to the element's Y position
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();

Try to click using X coordinates

WebElement elementToClick = driver.findElement(By.xpath("Your xpath"));
// Scroll the browser to the element's X position
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().x+")");
// Click the element
elementToClick.click();

Hope this helps you

Community
  • 1
  • 1
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
  • And what about use this function for any click error exception (see my update) ? – david hol May 23 '16 at 12:13
  • Other element will get the click error simply means element is present inside the DOM but still not has a fixed position, so if you think your function is well enough to handle above point then its fine – Rajnish Kumar May 23 '16 at 12:21
  • And how to fix this fixed position ? – david hol May 23 '16 at 12:24
  • we don't have to do anything to get element fix position it simply needs some time hence usually i perform some other operations before i click also i have seen that using css selector solves this issue generally (it smy observation simply) – Rajnish Kumar May 23 '16 at 12:26
  • hi please go through the link that i have shared there i have mentioned what operation we can perform – Rajnish Kumar May 23 '16 at 12:29
  • 1.Instead of performing operation's directly at the target area try to do some extra/false activity with webdriver which will give time for DOM to position all of his elements - what does it mean ?? 2.apply Thread.sleep() - please see my update, it it OK ? (i aded sleep before and inside catch) 3. also if you are running your test in smaller window size then set the size to maximum it - Done. – david hol May 23 '16 at 12:35
  • Point 1 means perform some extra activity [even if its not required] it will eventually buy some time for DOM to give its element fixed position.also if Point 2 and 3 helped ? – Rajnish Kumar May 23 '16 at 12:49
  • I need to check visibilityOf(WebElement element) and visibilityOfElementLocated(By locator) together or the second is enough ? regards to click using X coordinates and using Y coordinates: again - try it only if the regular click failed ? i need to try each by alone or together ? – david hol May 23 '16 at 13:13
  • use visibilityOfElementLocated(By locator) i thinks its better in our case,yes try only when regular click fails,you can use them alone – Rajnish Kumar May 23 '16 at 13:17
  • You think that using visibilityOfElementLocated is better then elementToBeClickable when i want to click button ??? – david hol May 23 '16 at 13:23
  • as per official document 1. visibilityOf(WebElement element) An expectation for checking that an element, known to be present on the DOM of a page, is visible. 2.visibilityOfElementLocated(By locator) An expectation for checking that an element is present on the DOM of a page and visible. os i guess option 2 is good – Rajnish Kumar May 23 '16 at 13:33
  • I mean elementToBeClickable instead of visibilityOfElementLocated(By locator) in case i want to click of course – david hol May 23 '16 at 13:49
  • thats odd because i think elementToBeClickable is especially for clicks – david hol May 23 '16 at 14:04
  • yes you are right but my answer is in current context of the question 1.elementToBeClickable(By locator) An expectation for checking an element is visible and enabled such that you can click it. 2.visibilityOfElementLocated(By locator) An expectation for checking that an element is present on the DOM of a page and visible. – Rajnish Kumar May 23 '16 at 14:08