简体   繁体   中英

Using Selenium, is there another, more reliable, way to use click command on an element in Headless Chrome?

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():

Eg: 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(); //CLICK() SEEMS VERY ERRATIC ON HEADLESS CHROME

 if(wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElementAfterClick)).isDisplayed() == true)
                break;
          } catch (TimeoutException toe) {
                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.

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM