简体   繁体   English

Selenium DoubleClick WebElement导致错误

[英]Selenium DoubleClick WebElement causing Error

I am using the code below to stimulate a double mouse click on a button, however, I am getting a compile error which is pointing towards the actions but I don't no how to fix it. 我正在使用下面的代码来激发双击鼠标的按钮,但是,我遇到了一个编译错误,该错误指向操作,但是我没有解决方法。

Actions act = new Actions(driver);
    act.doubleClick(driver.findElement(By.id("dijit_form_Button_0_label"))).build().perform();
    logger1.info("Logout Successful");

org.testng.TestNGException: 
Cannot instantiate class testNG.RAD3398LogoutTwiceTest
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.NullPointerException
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
    at testNG.RAD3398LogoutTwiceTest.<init>(RAD3398LogoutTwiceTest.java:25)
    ... 26 more

Any ideas? 有任何想法吗?

you could try to fire the doubleclick event using javascript. 您可以尝试使用javascript触发doubleclick事件。 This is how I do for triggering the click event from selenium, I guess that you can adapt it for triggering a doubleclick 这是我从硒触发click事件的方法,我想您可以将其调整为触发doubleclick

    public static void phantomClick (WebDriver driver, WebElement element){
        final String script = "function ghostclick(el){var ev = document.createEvent(\"MouseEvent\");ev.initMouseEvent(\"click\",true ,true,window,null,0,0,0,0,false,false,false,false,0,null);el.dispatchEvent(ev);} return ghostclick(arguments[0])";
        ((JavascriptExecutor) driver).executeScript(script, element);
    }   

A TestNGException isn't a compile error. TestNGException不是编译错误。 It is a RuntimeException thrown by TestNG. 它是TestNG抛出的RuntimeException In this case TestNG threw the exception because it could not "instantiate class testNG.RAD3398LogoutTwiceTest". 在这种情况下,TestNG引发了异常,因为它无法“实例化类testNG.RAD3398LogoutTwiceTest”。

Here is the reason why: 原因如下:

Caused by: java.lang.NullPointerException
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
    at testNG.RAD3398LogoutTwiceTest.<init>(RAD3398LogoutTwiceTest.java:25)

This means that when initializing/constructing an instance of RAD3398LogoutTwiceTest that a constructor for Actions was invoked which then threw a NullPointerException on line 44. 这意味着在初始化/构造RAD3398LogoutTwiceTest的实例时,将调用Actions的构造函数,然后在第44行上抛出NullPointerException

If you take a look at Actions.java:44 you'll see this.mouse = ((HasInputDevices) driver).getMouse(); 如果您看一下Actions.java:44,您将看到this.mouse = ((HasInputDevices) driver).getMouse(); . ie The initialization failed because driver is null . 即初始化失败,因为drivernull

Make sure that any statements like new Actions(driver); 确保任何语句,例如new Actions(driver); occur after driver is initialized or your test class will fail to initialize and TestNG will throw an exception. driver初始化后发生,否则您的测试类将无法初始化,并且TestNG将引发异常。

Once you fix this issue your statement to double-click should execute. 解决此问题后,应执行双击语句。

Alternative workaround Source 替代解决方法源

Simplified to this: 简化为:

((JavascriptExecutor) driver).executeScript("document.getElementById('map_container').dispatchEvent(new Event('dblclick'));"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM