Selenium DoubleClick WebElement导致错误

时间:2016-01-11 10:02:17

标签: java selenium selenium-webdriver testng

我正在使用下面的代码来刺激双击按钮,但是,我收到的编译错误指向了操作,但我不知道如何修复它。

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

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

您可以尝试使用javascript触发doubleclick事件。 这就是我从selenium触发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);
    }   

答案 1 :(得分:0)

TestNGException不是编译错误。这是TestNG抛出的RuntimeException。在这种情况下,TestNG抛出了异常,因为它无法实例化类testNG.RAD3398LogoutTwiceTest&#34;。

原因如下:

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

这意味着在初始化/构建RAD3398LogoutTwiceTest的实例时,调用Actions的构造函数,然后在第44行抛出NullPointerException

如果你看一下Actions.java:44,你会看到this.mouse = ((HasInputDevices) driver).getMouse();。即初始化失败,因为drivernull

确保在初始化new Actions(driver);之后发生driver之类的任何语句,否则您的测试类将无法初始化,而TestNG将抛出异常。

解决此问题后,您的双击语句应该执行。

答案 2 :(得分:0)

Alternative workaround Source

简化为:

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