- 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
Is it possible to handle Windows based pop-ups in Selenium?
Yes, it is possible to handle Windows based pop-ups in Selenium webdriver. Sometimes on clicking a link or a button, another window gets opened. It can be a pop up with information or an advertisement.
The methods getWindowHandles and getWindowHandle are used to handle child windows. The getWindowHandles method stores all the handle ids of the opened windows in the form of Set data structure.
The getWindowHandle method stores the handle id of the window in focus. Since the getWindowHandles method holds all the opened window handle ids, we can iterate through these handle ids with the iterator and next methods.
To switch to a specific window, switchTo.().window() method can be used. The handle id of the window where we want to switch is passed as a parameter to this method.
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 FirstAssign { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "chromedriver"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //url launch driver.get("https://secure.indeed.com/account/login"); driver.findElement(By.id("login-google-button")).click(); //hold window handles Set<String> s = driver.getWindowHandles(); // iterate handles Iterator<String> i = s.iterator(); //child window handle id String c = i.next(); //parent window handle id String p = i.next(); // child window switch driver.switchTo().window(c); System.out.println("Page title of child window: "+ driver.getTitle()); // switch to parent window driver.switchTo().window(p); System.out.println("Page title of parent window: "+ driver.getTitle()); //browser quit driver.quit(); } }
Output
- Related Articles
- Can selenium handle Windows based pop up?
- How to handle popup windows in Selenium?
- How to handle child windows in Selenium with python?
- How to handle web based alerts in Selenium?
- How to handle "Plugin blocked" pop up using Selenium Python?
- How to handle windows file upload using Selenium WebDriver?
- How to create Message Pop-Ups with Java?
- How to Block Pop-Ups on Google Chrome?
- Is it possible to have a function-based index in MySQL?
- How to handle frames in Selenium?
- How to Handle alerts in Selenium?
- How to create input Pop-Ups (Dialog) and get input from user in Java?
- How to handle chrome notification in Selenium?
- How to Remove YOUR COMPUTER HAS BEEN BLOCKED Fake Microsoft Warning Pop Ups?
- How to handle proxy in Selenium in Java?
