16

I have some tests which click on a tab, however the click is not always performed.

  • The xpath is correct as most of the times the test works

  • It is not a timing issue as I ve used thread.sleep() and other methods to ensure that the element is visible before clicking

  • The test believes that it is performing the click as it is not throwing an ElementNotFoundException or any other exceptions when 'performing' the click. The test fails later on after the click since the tab content would not have changed.

Further Info I am using Selenium 2.44.0 to implement tests in Java which run on Chrome 44.0.2403.107 m.

Is there something else that I can do or could this be an issue with selenium?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Jeremy Borg
  • 413
  • 2
  • 6
  • 16
  • 1
    jeremy -chrom v44 has known issues with click events and various sendkey combinations (in other words, it;s fairly useless). there has been lots of chatter about this, as this is the 1st time that chrome has majorly slipped in this respect. many of my tests are faling now too and I didn't realise the issue and was adding a whole new bundle of Waits until I discovered that there was a problem with v44 -hope this helps – jim tollan Jul 30 '15 at 17:03
  • 1
    Thank you for your reply. I guess we'll have to wait for a new version of chrome then. – Jeremy Borg Aug 05 '15 at 14:15

5 Answers5

31

There are several things you can try:

  • an Explicit elementToBeClickable Wait:

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

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

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", button);
    
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    alec, really nioce summary of the various approaches - plus one! – jim tollan Jul 30 '15 at 17:04
  • 2
    None of these approaches work for me. Except js method, the click is registered on the element and the element is highlighted by a dotted border after being clicked. But, nothing happens. When i click the same button manually, then the click works. – MasterJoe Mar 25 '17 at 01:33
  • 2
    The suggestions from @sumitkumarpradhan are `driver.findElement(button).sendKeys(Keys.RETURN);` and `driver.findElement(button).sendKeys(Keys.ENTER);`, if you don't feel like clicking links. – brandones May 05 '20 at 23:40
  • any ideas why the first and the second solutions does not work always (i created a while loop and it only works in 95%) but the last always works? In my case it is the first click right after the page is loaded, might it be a chrome bug? – wutzebaer Dec 17 '20 at 22:55
  • @wutzebaer the reason why the last one always works is that it is a programmatic way of achieving the same thing..I do prefer using this approach if I am using selenium for data gathering and not testing.. – alecxe Dec 17 '20 at 22:57
1

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

Aditya Byreddy
  • 101
  • 1
  • 10
1

The following method work for me

WebElement button = SeleniumTools.findVisibleElement(By.cssSelector("#cssid"));

Actions actions = new Actions(driver);

actions.moveToElement(button).click().build().perform();
Abhinav Sood
  • 799
  • 6
  • 23
Vilo C
  • 11
  • 1
1

I have a similar problem. Tried all solutions from the top answer. Sometimes they work, sometimes don't.

But running code in an infinite loop works always.

For example, we need to click on element-two which is not visible until element-one is clicked.

WebDriverWait wait = new WebDriverWait(webDriver, 10);
while (true){
    try {
        WebElement elementOne = 
              wait.until(ExpectedConditions.elementToBeClickable(By.id("element-one")));
        elementOne.click();
        WebElement elementTwo = 
              wait.until(ExpectedConditions.elementToBeClickable(By.id("element-two")));
        elementTwo.click();
        break;
    } catch (Exception e){
        //log
    }

}
Yuriy N.
  • 4,936
  • 2
  • 38
  • 31
0

I have a similar problem. Here is my solution:

table_button = driver.find_element(By.XPATH, insert your xpath)
try:
    WebDriverWait(driver, 15).until(EC.element_to_be_clickable(table_button)).click()
except WebDriverException as e:
    print('failed')
    print(e)

Through code above, you can find the error message if your button is not clickable.

For example, my error message is 'nosuchelement' and 'clcik is not clickable', then I got back to check the table_button.accessible_name, found it print a 'null' value, so that means my XPATH is incorrect.

Hope
  • 11
  • 3