Selenium click doesn't always work

I have several tests that click on a tab, however the click is not always executed.

  • xpath is correct as most of the time when the test is running

  • This is not a synchronization issue, as I used thread.sleep () and other methods to make sure the element is visible before clicking

  • The test considers that it executes a click because it does not throw an ElementNotFoundException or any other exceptions when it “clicks”. The test will fail later after clicking, because the contents of the tab has not changed.

Additional information I use Selenium 2.44.0 to implement Java tests that run on Chrome 44.0.2403.107 m.

Is there anything else I can do, or could it be a problem with selenium?

+5
source share
3 answers

There are a few things you can try:

  • a Explicit elementToBeClickable Wait:

     WebDriverWait wait = new WebDriverWait(webDriver, 10); WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("myid"))); button.click() 
  • go to the element before making a click:

     Actions actions = new Actions(driver); actions.moveToElement(button).click().build().perform(); 
  • click through javascript:

     JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("arguments[0].click();", button); 
+17
source

you can go with linkText if the tab name contains any unique string. And make sure the tab is not dynamic. It should be visible in the source code (manual source code ( ctrl+u )).

0
source

The following method works for me

 WebElement button = SeleniumTools.findVisibleElement(By.cssSelector("#cssid")); Actions actions = new Actions(driver); actions.moveToElement(button).click().build().perform(); 
0
source

All Articles