如何使用 Java 在 Selenium WebDriver 中关闭子浏览器窗口 [英] How to close child browser window in Selenium WebDriver using Java

查看:27
本文介绍了如何使用 Java 在 Selenium WebDriver 中关闭子浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到新窗口并完成任务后,我想关闭那个新窗口并切换到旧窗口,

After I switch to a new window and complete the task, I want to close that new window and switch to the old window,

所以我在这里写了类似代码:

so here i written like code:

// Perform the click operation that opens new window

String winHandleBefore = driver.getWindowHandle();

    // Switch to new window opened

    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }

    // Perform the actions on new window


    driver.findElement(By.id("edit-name")).clear();
    WebElement userName = driver.findElement(By.id("edit-name"));
    userName.clear();
              try
    {
        driver.quit();
    }

    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("not close");
                }

driver.switchTo().window(winHandleBefore);// Again I want to start code this old window

上面我写了代码driver.quit()driver.close().但我收到错误.谁能帮帮我...?

Above I written code driver.quit() or driver.close(). But I am getting error. Can anybody help me...?

org.openqa.selenium.remote.SessionNotFoundException: 调用 quit() 后无法使用 FirefoxDriver.

org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.

推荐答案

关闭单个浏览器窗口:

driver.close();

关闭所有(父+子)浏览器窗口并结束整个会话:

To close all (parent+child) browser windows and end the whole session:

driver.quit();

这篇关于如何使用 Java 在 Selenium WebDriver 中关闭子浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆