- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can selenium handle Windows based pop up?
Selenium can handle Windows based pop up. There may be scenarios where a web page opens more than one window after performing some actions on it. The child windows that get opened may be a pop up containing some information or advertisement.
Selenium uses the getWindowHandles () and getWindowHandle () methods to work with child windows. The getWindowHandles () method contains all the window handle ids of the opened windows. The window id handles are held in the form of Set data structure [containing data type as String].
The getWindowHandle () method is used to store the window handle id of the present active window. As we know, getWindowHandles () method is used to store all the opened window handle ids. To iterate through all the handles, iterator () and next () methods are used.
Since getWindowHandles () stores the window ids in the form of Set data structure, we have to import java.util.Set in our code. Also, for using the iterator () method, we have to import java.util.Iterator and import java.util.List.
Finally to switch to a particular window, switchTo.().window() method is used. The handle id of the window where we want to switch is passed as an argument to that method.
The steps to be followed to implement the above concept −
After the application is launched, let us first store all the window handle ids in a Set data structure with the help of getWindowHandles () method.
We shall iterate through all the window handle ids with the help of iterator () and next () methods.
Let us then grab the current window handle id with the help of getWindowHandle () method.
Then switch to that window with switchTo.().window() 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 HandleWindows { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://secure.indeed.com/account/login"; driver.get(url); //implicit wait driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // store all window handles Set<String> a = driver.getWindowHandles(); // iterate through handles Iterator<String> it = a.iterator(); String chlwnd = it.next(); String pwnd = it.next(); // switch to child window driver.switchTo().window(chlwnd); System.out.println("Page title "+ driver.getTitle()); // switch to parent window driver.switchTo().window(pwnd); System.out.println("Page title "+ driver.getTitle()); driver.quit();
- Related Articles
- Is it possible to handle Windows based pop-ups in Selenium?
- How to handle "Plugin blocked" pop up using Selenium Python?
- How to handle popup windows in Selenium?
- How to handle windows file upload using Selenium WebDriver?
- How to handle child windows in Selenium with python?
- How to handle web based alerts in Selenium?
- How to close the pop up window in selenium running?
- How to avoid the pop-up window in chrome browser with Selenium?
- How to automate menu box/pop up of right click in Python Selenium?
- How do I automatically download files from a pop up dialog using selenium-python?
- How can I pop-up a print dialog box using JavaScript?
- How can I handle multiple keyboard keys using Selenium Webdriver?
- How can we handle authentication popup in Selenium WebDriver using Java?
- How I can replace a JavaScript alert pop up with a fancy alert box?
- Fetch value from Alert pop up in jQuery
