
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to close child browser window in Selenium WebDriver using Java?
We can close the child browser window in Selenium webdriver. The getWindowHandles and getWindowHandle methods can be used to handle child windows. The getWindowHandles method is used to store all the opened window handles in the Set data structure.
The getWindowHandle method is used to store the browser window currently active. To iterate over the window handles, the iterator method is used. We have to add import java.util.Set to accommodate Set and import java.util.List and import java.util.Iterator statements to accommodate iterator in our code.
By default, the driver object can access the elements of the parent window. In order to switch its focus from the parent to the child window, we shall take the help of the switchTo().window method and pass the window handle id of the child window as an argument to the method. Then to move from the child window to the parent window, we shall take the help of the switchTo().window method and pass the parent window handle id as an argument to the method.
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; import java.util.Set; import java.util.Iterator; public class CloseChildWindow { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://secure.indeed.com/account/login"); //implicit wait driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); driver.findElement(By.id("login-google-button")).click(); // window handles Set w = driver.getWindowHandles(); // window handles iterate Iterator t = w.iterator(); String ch = t.next(); String pw = t.next(); // switching child window driver.switchTo().window(ch); System.out.println("Child window title "+ driver.getTitle()); // close the child browser window driver.close(); // switching parent window driver.switchTo().window(pw); System.out.println("Parent window title: "+ driver.getTitle()); driver.quit();
Output
- Related Questions & Answers
- How can I close a specific window using Selenium WebDriver with Java?
- How to maximize the browser window in Selenium WebDriver using C#?
- How to close the whole browser window by keeping the webDriver active?
- How to open browser window in incognito/private mode using python selenium webdriver?
- How to maximize or minimize a browser window using Selenium webdriver with Python?
- How to Resize Browser Window in WebDriver?
- How to open a browser window in full screen using Selenium WebDriver with C#?
- Capturing browser logs with Selenium WebDriver using Java.
- How to open a new window on a browser using Selenium WebDriver for python?
- How to hide Firefox window (Selenium WebDriver)?
- How to connect to an already open browser using Selenium Webdriver?
- How to close a browser session in Selenium with python?
- How to launch Edge browser with Selenium Webdriver?
- Clear browser Cookies with Selenium WebDriver Java bindings.
- How to close the pop up window in selenium running?