如何通过保持webDriver处于活动状态来关闭整个浏览器窗口? [英] How to close the whole browser window by keeping the webDriver active?

查看:330
本文介绍了如何通过保持webDriver处于活动状态来关闭整个浏览器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的批处理执行中,针对第一种情况打开了具有多个选项卡的多个浏览器.我想在开始第二种情况之前关闭所有这些浏览器.

In my batch execution, multiple browsers with multiple tabs are getting opened for first scenario. I wanted to close all these browsers before starting second scenario.

Driver.close()只是关闭浏览器的一个标签. Driver.quit()正在关闭所有浏览器,并同时结束WebDriver会话.因此,无法运行批处理执行.请为此提供解决方案.

Driver.close() is just closing one tab of the browser. Driver.quit() is closing all the browsers and also ending the WebDriver session. So , am unable to run the batch execution. Please provide a solution for this.

推荐答案

以下说明应解释WebDriver中 driver.close driver.quit 方法之间的区别.我希望您觉得它有用.

The below explanation should explain the difference between driver.close and driver.quit methods in WebDriver. I hope you find it useful.

driver.close driver.quit 是在Selenium WebDriver中关闭浏览器会话的两种不同方法.

driver.close and driver.quit are two different methods for closing the browser session in Selenium WebDriver.

了解两者以及了解何时使用每种方法对您的测试执行很重要.因此,我试图阐明这两种方法.

Understanding both of them and knowing when to use each method is important in your test execution. Therefore, I have tried to shed some light on both of these methods.

driver.close -此方法关闭设置焦点的浏览器窗口. driver.quit ,同时关闭webdriver的会话 driver.close 仅在没有其他窗口打开且您调用时,仅关闭存在硒控件但当前尚未关闭webdriver会话的当前窗口 driver.close ,然后它也将关闭webdriver会话.

driver.close - This method closes the browser window on which the focus is set. driver.quit close the session of webdriver while driver.close only close the current window on which selenium control is present but webdriver session not close yet, if no other window open and you call driver.close then it also close the session of webdriver.

driver.quit –该方法基本上调用driver.dispose一个内部方法,该方法随后关闭所有浏览器窗口并关闭 正常结束WebDriver会话.

driver.quit – This method basically calls driver.dispose a now internal method which in turn closes all of the browser windows and ends the WebDriver session gracefully.

driver.dispose -如前所述,是WebDriver的一种内部方法,已根据另一个答案将其静默删除-需要验证.在正常的测试工作流程中,此方法确实没有用例,因为上述两种方法中的任何一种都适用于大多数用例.

driver.dispose - As mentioned previously, is an internal method of WebDriver which has been silently dropped according to another answer - Verification needed. This method really doesn't have a use-case in a normal test workflow as either of the previous methods should work for most use cases.

说明用例:每当您要结束程序时,都应使用 driver.quit .它将关闭所有打开的浏览器窗口,并终止WebDriver会话.如果在程序末尾不使用driver.quit,则WebDriver会话将无法正确关闭,并且不会从内存中清除文件.这可能会导致内存泄漏错误.

Explanation use case: You should use driver.quit whenever you want to end the program. It will close all opened browser windows and terminates the WebDriver session. If you do not use driver.quit at the end of the program, the WebDriver session will not close properly and files would not be cleared from memory. This may result in memory leak errors.

............

............

现在,在这种情况下,您需要特定的浏览器. 下面的代码将关闭除主窗口之外的所有子窗口.

Now In that case you need to specific browser. Below is code which will close all the child windows except the Main window.

String homeWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();

//Use Iterator to iterate over windows
Iterator<String> windowIterator =  allWindows.iterator();

//Verify next window is available
while(windowIterator.hasNext())
{
    //Store the Recruiter window id
    String childWindow = windowIterator.next();
}

//Here we will compare if parent window is not equal to child window 
if (homeWindow.equals(childWindow))
{
    driver.switchTo().window(childWindow);
    driver.close();
}

现在,您需要根据需要修改或添加条件

Now here you need to modify or add the condition according to your need

if (homeWindow.equals(childWindow))
{
    driver.switchTo().window(childWindow);
    driver.close();
}

当前,它仅检查主窗口是否等于子窗口.在这里,您需要指定条件,例如要关闭的ID.我从来没有尝试过,所以只是建议您达到要求的方式.

Currently it is checking only if home window is equal to childwindow or not. Here you need to specify the condition like which id's you want to close. I never tried it so just suggested you the way to achive your requirement.

这篇关于如何通过保持webDriver处于活动状态来关闭整个浏览器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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