Click on disabled element using Actions class doesnt work

眉间皱痕 提交于 2019-12-08 08:10:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!