问题
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:
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:
- The Locator Strategies you have used, uniquely idendentifies the element within the DOM Tree.
- JDK is upgraded to current levels JDK 8u222.
- Selenium is upgraded to current levels Version 3.141.59.
- ChromeDriver is updated to current ChromeDriver v77.0 level.
- Chrome is updated to current Chrome Version 77.0 level. (as per ChromeDriver v77.0 release notes)
回答2:
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()
来源:https://stackoverflow.com/questions/58009515/using-selenium-is-there-another-more-reliable-way-to-use-click-command-on-an