Bhargav
Bhargav

Reputation: 25

Click on disabled element using Actions class doesnt work

I have a test where I need to click on a disabled button. I am using the Actions class to do this. When the user clicks on the button, an alert is generated. Below is the code i have written:

Actions mouseActions = new Actions(driver);
mouseActions.moveToElement(driver.findElement(By.id("disabled_element_id"))).click().build().perform();

Then I try to switch to the alert I get exception: Exception in thread "main" org.openqa.selenium.NoAlertPresentException: No alert is present.

Upvotes: 1

Views: 9722

Answers (2)

Petr Mensik
Petr Mensik

Reputation: 27496

You need to use JavaScriptExecutor for this task, WebDriver is not able to click on elements which are disabled or invisible. So try something like

JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("document.querySelector(\"button[id=yourButton]\").click()");

Upvotes: 3

Robbie Wareham
Robbie Wareham

Reputation: 3438

Selenium has been written to replicate user interaction, therefore will not allow interaction with disabled objects as a human would not be able to either.

you can either;

  • Replicate the process a user would do to enable a button.

  • Use JavaScript to enable or perform the interaction

Upvotes: 2

Related Questions