单击使用Actions类的禁用元素不起作用 [英] Click on disabled element using Actions class doesnt work

查看:81
本文介绍了单击使用Actions类的禁用元素不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试,需要单击禁用的按钮。
我正在使用Actions类来执行此操作。
当用户单击按钮时,将生成警报。
以下是我编写的代码:

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

然后我尝试切换到警报,我得到异常:
线程 main中的异常 org.openqa.selenium.NoAlertPresentException:不存在警报。

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

推荐答案

您需要使用 JavaScriptExecutor 对于此任务, WebDriver 无法单击已禁用或不可见的元素。因此,尝试类似

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

这篇关于单击使用Actions类的禁用元素不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆