0

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.

  • 1
    Please tell us what happens when it doesn't work. You say that click() is "erratic", but you didn't tell us in what way is it erratic. – Breaks Software Sep 19 '19 at 11:05
  • The click() command always executes successfully (otherwise a NoSuchElement 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. – Sakhile Vinny Daweti Sep 19 '19 at 11:24

2 Answers2

1

I've had rare circumstances like this too. You might try using ActionChains to do the click instead. It's been a while since I wrote anything in Java, but in Python, the code would look something like this:

# find your element using WebDriverWait, as you have above
ActionChains(self.driver).move_to_element(element).click().perform()
Breaks Software
  • 1,721
  • 1
  • 10
  • 15
1

To keep it short and simple if your usecase is to invoke click() on a certain WebElement presenceOfElementLocated() and visibilityOf() won't help and you need to induce WebDriverWait for the elementToBeClickable() and you can use the following solution:

try {
  new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element).click();
  System.out.println("Element was clicked")
}
catch(TimeoutException e) {
  System.out.println("Element wasn't clicked")
}

You can find a detailed discussion in Selenium: Check for the presence of element

Additional consideration

Ensure that:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • having only the elementToBeClickable() ExpectedCondition, which verifies the element is present (whether visible or not) on the DOM, was my first stab until the "unstableness" of my tests... Then I slowly added the other 2 for accuracy. Yet, my tests were still unstable. – Sakhile Vinny Daweti Sep 19 '19 at 12:38
  • Instability can occur due to various reasons, I have covered a few in my answer. You need to be a bit specif what exactly you mean by _unstable_ – undetected Selenium Sep 19 '19 at 12:43
  • Good point! I've edited and tried to expand more on my question. – Sakhile Vinny Daweti Sep 19 '19 at 13:51