After 2 days of struggling with an erratic click() command issue in Headless Chrome - in my case an Anchor (a) element with a href tag - and going through various threads advice about chromeOptions (--start-maximized, --window-size, etc...), and trying 12 different ways ( with sendKeys and Actions, and even submit() ) to work around the click() with no success...
Using ChromeDriver 77.0, Chrome 77.0.3865.75 and Selenium 3.141.59, my tests are stable in Chrome and they're unstable in Headless Chrome because of this erratic click():
E.g.: Click on an element (in my case an anchor (a) element with a href tag), in both Chrome and Headless Chrome, and check another element appears thereafter
Adding the loop and try catch below stabilizes my tests and makes their results reliable! Is there another way you can think of?
Test() {
for(int t = 0; t <= 2; t++) { //TRY CLICKING ON THE ELEMENT 3 TIMES
WebElement element =
wait.until(ExpectedConditions.presenceOfElementLocated(elementToFind));
wait.until(ExpectedConditions.visibilityOf(element));
wait.until(ExpectedConditions.elementToBeClickable(element));
try {
element.click(); //ERRATIC CLICK() ON HEADLESS CHROME
if(wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElementAfterClick)).isDisplayed() == true)
break; //BUTTON WAS REALLY CLICKED
} catch (TimeoutException toe) { //BUTTON WASN'T REALLY CLICKED
if (t == 2) toe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
I'm saying "the click() is erratic in Headless Chrome" because the click() command always executes successfully (otherwise a NoSuchElement or a StaleElementReferenceException or any other exception would be found when clicking on the element) BUT, sometimes, the element is not actually clicked. Other times, the very same test and code runs smoothly and the element is actually clicked - I know this because the line with visibilityOfElementLocated(expectedElementAfterClick) executes as expected. This click() issue, in turn, makes my tests unstable. Thus, the results unreliable.
I suspect this to be an actual Selenium issue.