首页 > 解决方案 > org.openqa.selenium.NoAlertPresentException:通过 Selenium 调用 Actions 类方法 sendKeys 时没有打开警报

问题描述

编辑:我找到了解决方案。您不能在 Selenium 中使用右键单击与本机上下文菜单,只能使用自定义上下文菜单

更新代码以右键单击自定义上下文菜单,它工作正常。

我查看了十几个解决方案,但没有一个有效,所以我必须分享我的代码。请注意,此解决方案适用于我的 Mac。它不适用于我的工作笔记本电脑(Windows)。我正在使用 Chrome、Java 8 和 Selenium 3.8.1

@Test
public void contextClickTest() {
    WebElement contextRegion;

    // Arbitrary collection of Keys to execute to navigate context menu
    Keys[] keys = { Keys.ARROW_DOWN, Keys.ENTER };

    // this site has a good context click demo
    driver.get("https://swisnl.github.io/jQuery-contextMenu/demo.html");

    // get the context region of the button using xpath
    contextRegion = driver.findElement(By.xpath("/html/body/div/section/div/div/div/p/span"));

    // Perform action to select some value in the context menu
    Actions act = new Actions(driver).moveToElement(contextRegion).contextClick(contextRegion);
    for(CharSequence cs : keys) {
        act = act.sendKeys(cs);
    }
    act.build().perform();

    // if successful an alert will appear. 
    // If not, this will throw an exception
    driver.switchTo().alert().accept();
}

在另一台机器上运行时无法工作的原因可能是什么?我需要为不同的平台使用不同的密钥吗?

编辑:这是打开警报窗口失败引发的异常:

    FAILED: contextClickTest
org.openqa.selenium.NoAlertPresentException: no alert open
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.12.6 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'Seans-MacBook-Pro.local', ip: '10.0.0.104', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', java.version: '1.8.0_92'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptSslCerts: true, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.33.506106 (8a06c39c4582fb..., userDataDir: /var/folders/h7/0637cbz907d...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 68.0.3440.106, webStorageEnabled: true}
Session ID: d7788e0a9788a597df727cfbaa4a26bd
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658)
    at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.alert(RemoteWebDriver.java:987)
    at com.revature.testsuites.ContextClickTest.contextClickTest(ContextClickTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
    at org.testng.TestNG.run(TestNG.java:1031)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

标签: seleniumselenium-webdriverwebdriverselenium-chromedrivercontextmenu

解决方案


你可以通过 try-catch 来处理它,它会抛出 Alert Exception,

示例:

try {
    driver.switchTo().alert().accept();
} catch (Exception Ex) {
    System.out.println("Alert was not present");
}

推荐阅读