Jeremy Borg
Jeremy Borg

Reputation: 413

Selenium click not always working

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

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?

Upvotes: 16

Views: 18714

Answers (5)

Hope
Hope

Reputation: 9

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.

Upvotes: -1

Yuriy N.
Yuriy N.

Reputation: 6137

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
    }

}

Upvotes: 1

Vilo C
Vilo C

Reputation: 9

The following method work for me

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

Actions actions = new Actions(driver);

actions.moveToElement(button).click().build().perform();

Upvotes: 0

Aditya Byreddy
Aditya Byreddy

Reputation: 99

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

Upvotes: 0

alecxe
alecxe

Reputation: 474191

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

Upvotes: 32

Related Questions