1

This is my code

do {
    try {
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpLand)));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        driver.findElement(By.xpath(xpLand)).click();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Thread.sleep(500);
    if (i > 20 ) {
        break;
    }
    i++;
} while (driver.findElements(By.xpath(xpLand)).size() > 0);

URL https://win.milwaukeetool.eu/#

elementToBeClickable works (I have also tried different versions of visible and other alternatives), no exception is thrown. click also works, but nothing is triggered by the click. Finally, the while-condition is also true.

What happens is that the page loads (outside of the code above), clickable is satisfied, click doesn't throw an exception, the while-condition is greater than 0 (if the click would have worked a new page would load and it would be 0), and then it starts over.

Screen size 1440 x 900-23 (23 = height of menubar) xpLand = //img [@src='/img/countries/se.svg']

The weird thing is that it works about half of the time but fails the other half (I just run it around 400 times and it worked and failed about 200 times each). If I manually click the link when my code is stuck, the click leads to the expected result.

It is like it sometimes is getting stuck. I have an outer loop (not visible above) that runs ten times with the same driver and after that launches a new driver. If it "gets stuck", it won't work again until these ten loops are finished and the new driver is launched. Then it might work for 5-10 times, until it again gets stuck.

Using Chrome/chromedriver.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

0

You need to try the javascript executor click

WebElement button =driver.findElement(By.id("btn"));

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("arguments[0].click();", button);

0

In your first try/catch{} block instead of just inducing WebDriverWait invoke the click() once the element is returned as follows:

do {
    try {
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpLand))).click();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Thread.sleep(500);
    if (i > 20 ) {
        break;
    }
    i++;
} while (driver.findElements(By.xpath(xpLand)).size() > 0);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

An 'elegant' solution using JavascriptExecutor would be something like that:

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpLand)));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

When using JavascriptExecutor one might argue that 'its not really a user interaction',
It really is not a simulation of a user's interaction, BUT in some cases we do need a solid non-flaky solution to unexpected behaviour, and this does just that.

You could also wrap different click funtiions so you won't have to repeat them:

public void click(WebElement element) {
    wait.until(ExpectedConditions.elementToBeClickable(element)).click();
}
public void clickJS(WebElement element) {
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(element));
    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
}
Kfir Doron
  • 229
  • 2
  • 5
0

Please try the js click instead of Webdriver click as below

JavascriptExecutor executor = (JavascriptExecutor) driver; executor.executeScript("arguments[0].click();", xpLand);

selladurai
  • 29
  • 4