简体   繁体   English

如何使用Java在Selenium中选择一个窗口?

[英]How do I select a window in Selenium with Java?

While using the Firefox IDE, not quite everything is translatable. 在使用Firefox IDE时,并不是所有的东西都是可翻译的。 I'm having trouble with the following in particular: 我在以下方面遇到了麻烦:

ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=main | ]]

Can I just click on the window as I would any other button? 我可以像其他按钮一样单击窗口吗? My problem is that because this step is skipped, the automation test fails to click on the btnToDo element below it. 我的问题是,因为跳过了此步骤,所以自动化测试无法单击它下面的btnToDo元素。 I wonder if clicking the window is a prerequisite for clicking the button in this particular instance. 我想知道在此特定实例中单击窗口是否是单击按钮的先决条件。

driver.findElement(By.id("btnLogin")).click();
// ERROR: Caught exception [ERROR: Unsupported command [selectWindow | name=main | ]]
driver.findElement(By.id("btnToDo")).click();

Stack Trace: 堆栈跟踪:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"btnToDo"}
  (Session info: chrome=49.0.2623.110)
  (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.02 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'OLCZPR112319', ip: '10.99.133.66', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=C:\Users\MNXE\AppData\Local\Temp\scoped_dir7964_6827}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=49.0.2623.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: fda86deb9e9ba4514a3cd332a2bb73a3
*** Element info: {Using=id, value=btnToDo}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:413)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
    at ValidationTest.testValidation(ValidationTest.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

My error states that the button does not exist but it clearly does. 我的错误指出该按钮不存在,但显然存在。 After clicking "btnLogin", I am taken to a new page. 单击“ btnLogin”后,我被带到一个新页面。 Here, it seems the test is unable to locate the new "btnToDo". 在这里,似乎测试无法找到新的“ btnToDo”。

It looks like clicking on btnLogin, new window is populating as per provided recording code. 好像单击btnLogin一样,按照提供的记录代码填充新窗口。 So here we need to switch to that new window to interact with elements in that new window. 因此,在这里我们需要切换到该新窗口以与该新窗口中的元素进行交互。 So that switch window recorded in Selenium IDE is not translated to WebDriver. 因此,Selenium IDE中记录的切换窗口不会转换为WebDriver。

In webDriver there is concept getWindowHandles which provides to all web browsers opened by it. 在webDriver中,有一个getWindowHandles概念,它提供给它打开的所有Web浏览器。 So first collect those handles and iterate to new one which is opened, then switch to that. 因此,首先收集那些句柄并迭代到新的句柄,然后再切换到该句柄。 Once work in that window is done, if required you need to switch back to first window to interact elements in first window. 在该窗口中完成工作后,如果需要,您需要切换回第一个窗口以与第一个窗口中的元素进行交互。

for code please refer here 有关代码,请参阅此处

Thank You, Murali 谢谢,穆拉利

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

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