gpt4 book ai didi

java - 如何使用 Java 在 Selenium 中选择窗口?

转载 作者:行者123 更新时间:2023-12-01 10:05:38 25 4
gpt4 key购买 nike

使用 Firefox IDE 时,并非所有内容都可以翻译。我尤其在以下方面遇到问题:

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

我可以像单击其他按钮一样单击窗口吗?我的问题是,由于跳过了这一步,自动化测试无法单击其下面的 btnToDo 元素。我想知道在这个特定实例中单击窗口是否是单击按钮的先决条件。

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

堆栈跟踪:

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)

我的错误表明该按钮不存在,但它显然存在。单击“btnLogin”后,我将进入一个新页面。在这里,测试似乎无法找到新的“btnToDo”。

最佳答案

看起来就像单击 btnLogin,新窗口正在根据提供的录制代码填充。因此,这里我们需要切换到该新窗口以与该新窗口中的元素进行交互。因此 Selenium IDE 中记录的切换窗口不会转换为 WebDriver。

在 webDriver 中,有一个 getWindowHandles 概念,它提供给所有由它打开的 Web 浏览器。因此,首先收集这些句柄并迭代到打开的新句柄,然后切换到该句柄。一旦该窗口中的工作完成,如果需要,您需要切换回第一个窗口以与第一个窗口中的元素进行交互。

代码请引用here

谢谢你,穆拉利

关于java - 如何使用 Java 在 Selenium 中选择窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36496458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com