问题
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.
回答1:
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()");
回答2:
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
来源:https://stackoverflow.com/questions/18660917/click-on-disabled-element-using-actions-class-doesnt-work