我正在使用以下内容,
铬驱动程序 2.41
硒服务器独立-3.10.0.jar
JDK 1.8.0_141
我使用硒在chrome中打开多个选项卡,逐个遍历每个选项卡,然后执行一些脚本。但是,我在切换窗口手柄时经常遇到错误。在切换窗口之前使用System.out.println(driver.getWindowHandles())
,我发现窗口句柄的数量随着时间的推移而不断减少,并且org.openqa.selenium.NoSuchWindowException: no such window
。
另一个奇怪的事情是,代码将运行一次。然后它会为后续测试提供错误,但是如果我们将其保持原样十分钟后,它将再次正常运行。
代码实现如下:
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
for(int j=0; j<10; j++){
((JavascriptExecutor) driver).executeScript("window.open('" + *siteURL* + "')");
for(String temp : driver.getWindowHandles()){
if(!tabs.contains(temp)){
driver.switchTo().window(temp);
tabs.add(temp);
}
}
}
tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
/*** Reload tabs to reacquire the pages- Selenium loses them if it is idle for long time
* **/
for (int j = 0; j < tabs.size(); j++) {
driver.switchTo().window(tabs.get(j));
driver.get(*siteURL*);
}
tabs = new ArrayList<String>(driver.getWindowHandles());
for (int j = 0; j < tabs.size(); j++) {
driver.switchTo().window(tabs.get(j));
/*Perform step 1*/
}
错误:
org.openqa.selenium.NoSuchWindowException: no such window
(Session info: chrome=67.0.3396.87)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:11:12.151Z'
System info: host: 'APACSGAWSBU0130', ip: '10.58.196.139', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.41.578737 (49da6702b16031..., userDataDir: C:UsersHY84957Downloads...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:54006}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.87, webStorageEnabled: true}
Session ID: 0f98cc640eea21f2c48120f3bb901846
我遇到了同样的问题。getWindowHandles()
需要一些时间,Thread.sleep(500);
它工作正常。
public void goto2() throws InterruptedException {
ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
Thread.sleep(500);
driver.switchTo().window(winHandles.get(1));
}
Public void windowHandle()
{
Set<String> allWindowHandles ==driver.getwindowHandles();
Iterator<String> iterator = allWindowHandles.iterator();
while (iterator.hasNext()) {
String ChildWindow = iterator.next();
if (!mainWindowHandle.equalsIgnoreCase(ChildWindow)) {
driver.switchTo().window(ChildWindow);
WebElement text = driver.findElement(By.id("sampleHeading")) System.out.println("Heading of child window is " + text.getText());
}
}