Selenium Chromedriver 和 IEdriver No Such Window 异常
Selenium Chromedriver and IEdriver No Such Window exception
我在 selenium 中遇到了这个问题,原来的 window 正在关闭,而不是新的 window 是开放的和有针对性的。代码在 firefox 上运行流畅。但在 Chrome(非常快)和 IE 中,它会失败并出现异常:org.openqa.selenium.NoSuchWindowException:没有这样的 window。
我觉得跟测试速度有关系?那么,chrome 在关闭 window 时尝试超快?您实际上如何关闭新的 window 并切换回原始的并与之交互?
这是我的代码片段。
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String originalWindow = driver.getWindowHandle();
String newWindow;
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> stringIterator = windowHandles.iterator();
while (stringIterator.hasNext()) {
newWindow = stringIterator.next();
if (!originalWindow.equalsIgnoreCase(newWindow)) {
driver.switchTo().window(newWindow);
System.out.println("The title of the page is: “ + driverInstance.getTitle());
}
}
driver.close(); ///In here I should close the new window
driver.switchTo().window(originalWindow); ///In here I should switch back to the old window
使用新信息重做...
我不知道这是否可行,因为我对弹出窗口的处理不多,但我会从这样的事情开始。
public static void main(String[] args)
{
// navigate to the page that spawns popups
while (driver.getWindowHandles().size() == 1)
{
// we're waiting for a popup to be spawned
}
closePopups(driver);
}
private static void closePopups(WebDriver driver)
{
// closes all browser windows other than the main window, e.g. popups
// NOTE: it will not close other instances of the same browser, only those spawned by the current driver
String currentHandle = driver.getWindowHandle();
for (String handle : driver.getWindowHandles())
{
if (!handle.equals(currentHandle))
{
driver.switchTo().window(handle).close();
}
}
driver.switchTo().window(currentHandle);
}
你能试着等一下吗
尝试通过显式等待添加
void checkAlert() {
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
//exception handling
}
}
Or
implicit wait
driver.switchTo().window(newWindow);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS
);
Or
with thread sleep :
driver.switchTo().window(newWindow);
System.out.println(driver.getTitle());
Thread.sleep(3000);
我在 selenium 中遇到了这个问题,原来的 window 正在关闭,而不是新的 window 是开放的和有针对性的。代码在 firefox 上运行流畅。但在 Chrome(非常快)和 IE 中,它会失败并出现异常:org.openqa.selenium.NoSuchWindowException:没有这样的 window。
我觉得跟测试速度有关系?那么,chrome 在关闭 window 时尝试超快?您实际上如何关闭新的 window 并切换回原始的并与之交互?
这是我的代码片段。
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String originalWindow = driver.getWindowHandle();
String newWindow;
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> stringIterator = windowHandles.iterator();
while (stringIterator.hasNext()) {
newWindow = stringIterator.next();
if (!originalWindow.equalsIgnoreCase(newWindow)) {
driver.switchTo().window(newWindow);
System.out.println("The title of the page is: “ + driverInstance.getTitle());
}
}
driver.close(); ///In here I should close the new window
driver.switchTo().window(originalWindow); ///In here I should switch back to the old window
使用新信息重做...
我不知道这是否可行,因为我对弹出窗口的处理不多,但我会从这样的事情开始。
public static void main(String[] args)
{
// navigate to the page that spawns popups
while (driver.getWindowHandles().size() == 1)
{
// we're waiting for a popup to be spawned
}
closePopups(driver);
}
private static void closePopups(WebDriver driver)
{
// closes all browser windows other than the main window, e.g. popups
// NOTE: it will not close other instances of the same browser, only those spawned by the current driver
String currentHandle = driver.getWindowHandle();
for (String handle : driver.getWindowHandles())
{
if (!handle.equals(currentHandle))
{
driver.switchTo().window(handle).close();
}
}
driver.switchTo().window(currentHandle);
}
你能试着等一下吗
尝试通过显式等待添加
void checkAlert() {
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
//exception handling
}
}
Or
implicit wait
driver.switchTo().window(newWindow);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS
);
Or
with thread sleep :
driver.switchTo().window(newWindow);
System.out.println(driver.getTitle());
Thread.sleep(3000);