Java Selenium 如何关闭 Chrome 浏览器并打开新的浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37670087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:32:17  来源:igfitidea点击:

How can Selenium close Chrome browser and open new One

javaselenium-webdriverselenium-chromedriver

提问by Seimone

My scenario is to close the chrome browser and open a new one.

我的方案是关闭chrome浏览器并打开一个新浏览器。

public String openNewBrowserWindow() {
    this.log("Opening new Browser window...");
    String stringHandles;
    Set<String> previousWindows = driver.getWindowHandles();
    String previousHandle = driver.getWindowHandle();
    ((JavascriptExecutor)driver).executeScript("window.open();");
    Set<String> newWindows = driver.getWindowHandles();
    newWindows.removeAll(previousWindows);
    String newHandle = ((String)newWindows.toArray()[0]);
    stringHandles = previousHandle + ";" + newHandle;
    return stringHandles;
}

What I did is this:

我所做的是这样的:

String handlesA = generic.openNewBrowserWindow();
String[] handleA = handlesA.split(";");
generic.closeBrowser();
generic.switchToWindow(handleA[1]);

This works on firefox but not in chrome. Do you guys have any suggestion?

这适用于 Firefox,但不适用于 chrome。大家有什么建议吗?

回答by Riaz

Why not just:

为什么不只是:

driver.quit()
driver = new ChromeDriver()

What is your scenario really?

你的场景什么?

回答by Ashok kumar Ganesan

@Seimone Whenever you want to intiate a Chrome browser, system property must be defined to the chromedriver.exe

@Seimone 每当您想启动 Chrome 浏览器时,系统属性都必须定义到 chromedriver.exe

System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
WebDriver driver = new ChromeDriver();

Also, If you want to close your current chrome browser window use the following one in your code.

此外,如果您想关闭当前的 chrome 浏览器窗口,请在代码中使用以下代码。

driver.close();

If you want to close all your chrome browser window use the following one in your code.

如果要关闭所有 chrome 浏览器窗口,请在代码中使用以下代码。

driver.quit();

With reference to your scenario

参考你的场景

1.Open the url 2.Login with signed in. 3.Close the browser. 4.Open the browser and enter the same url. 5.Check the same user is logged in.

1.打开网址 2.登录并登录。 3.关闭浏览器。4.打开浏览器并输入相同的网址。5.检查登录的用户是否相同。

Try the below code and let me know your result.

试试下面的代码,让我知道你的结果。

String chromeDriver = "enter the chromedriver.exe path";
String chromeProfile = "C:/Users/MSTEMP/AppData/Local/Google/Chrome/User Data/Default"; //Local chrome profile path.
System.setProperty("webdriver.chrome.driver", chromeDriver);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir="+chromeProfile);
capabilities.setCapability("chrome.binary",chromeDriver);
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(capabilities);

driver.get("https://www.gmail.com");
/*write your login credentials code with username, password and perform the
login operation with signed in*/
driver.close();

//Now invoke the chrome browser and enter the url alone.
driver = new ChromeDriver(capabilities);
driver.get("http://www.gmail.com");
//write the code for user signed verification.