我的程序使用多个线程导航到不同的URL。它的工作方式是我的程序在每个浏览器中最多打开两个选项卡,然后(一旦达到限制)在新浏览器中打开下一组URL(而不仅仅是一个新选项卡)。每次我打开一个新选项卡(或浏览器)时,我都会检索当前的窗口句柄名称。但是,我注意到,当程序打开第二组URL时(在测试程序中,共有3个URL,因此第二个浏览器将仅包含1个URL /选项卡),窗口句柄与从窗口打开的第一个选项卡的窗口句柄匹配。第一盘。 硒似乎正在从第一个浏览器重用窗口句柄-为什么?
这是我的代码:
// 'subLists' contains two lists (1st list = 2 URLs, 2cnd list = 1 URL)
for (int sublistsCounter = 0; sublistsCounter < subLists.size(); sublistsCounter++) {
WebDriver driver = getNewFirefoxWebDriver();
if (driver != null) {
for (int sublistCounter = 0; sublistCounter < subList.size(); sublistCounter++) {
String url = sublist.get(sublistCouter).getURL();
String currentURLWindowHandle = "";
// Causes program to open new URL in new tab
if (sublistCounter > 0 ) {
// Opens URL via JS
navigateToURLInNewTabViaJS(driver, url);
// Returns window handle via getWindowHandle()
currentURLWindowHandle = getCurrentWindowHandle(driver);
}
else if (sublistCounter == 0) {
// Navigates to URL via driver.navigate.to() as this is the first URL in set
currentURLWindowHandle = navToPage(driver, url);
}
Set<String> windowHandles = getCurrentWindowHandles(driver);
// PRINTS: [4294967297] (1) [OK]
// PRINTS: [4294967297, 4294967317] (2) [OK]
// PRINTS: [4294967297] (1) [NOT OK]
System.out.println("Window Handles: " + windowHandles.toString() + " (" + windowHandles.size() + ")");
}
Set<String> windowHandles = driver.getWindowHandles();
// PRINTS: Window Handle Size: 2 (makes sense because first set contains 2 urls)
// PRINTS: Window Handle Size: 1 (makes sense bec second set contains only 1 URL)
// Next line executes total of two times (once for each sublist)
System.out.println("Window Handle Size: " + windowHandles.size());
}
}
从上面的代码中可以看到,第二个浏览器中的第一个选项卡与第一个浏览器中的第一个选项卡具有相同的窗口句柄(第二个浏览器只有一个选项卡,因为我们总共处理3个网址)。
给我的印象是Selenium
为每个选项卡/浏览器生成一个新的窗口句柄。
为什么Selenium重用窗口句柄?
谢谢