org.openqa.selenium.NoAlertPresentException:通过Selenium调用Actions类方法sendKeys时未打开警报

时间:2018-08-23 07:36:31

标签: selenium selenium-webdriver webdriver selenium-chromedriver contextmenu

编辑:我找到了解决方案。您不能在带有本机上下文菜单的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)

2 个答案:

答案 0 :(得分:0)

此错误消息...

org.openqa.selenium.NoAlertPresentException: no alert open

...表示 ChromeDriver 无法调用与 Action 类相关的方法。

您的主要问题是所使用的二进制版本之间的不兼容性

  • 您正在使用 chromedriver = 2.33
  • chromedriver=2.33的发行说明中明确提到以下内容:
  

支持 Chrome v60-62

  • 您正在使用 chrome = 68.0
  • ChromeDriver v2.41的发行说明中明确提到以下内容:
  

支持 Chrome v67-69

  • 您的 Selenium Client 版本是 2017-12-01T18:33:54.468Z 3.8.1 ,版本稍旧。 / li>
  • 您的 JDK版本 1.8.0_92 ,这是古老的

因此 JDK v8u92 Selenium Client v3.8.1 ChromeDriver v2.33 之间存在明显的不匹配Chrome浏览器v68.0

解决方案

答案 1 :(得分:0)

您可以通过try-catch处理它引发警报异常的方法,

示例示例:

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