Identifying and handling a pop-up window by its title
Many a times developers don't assign the name
attribute to pop-up windows. In such cases, we can use its window handle
attribute. However, the handle
attributes keep changing and it becomes difficult to identify the pop-up window, especially when there is more than one pop-up window open. Using the handle
and title
attributes of the page displayed in a pop-up window, we can build a more reliable way to identify the pop-up windows.
In this recipe, we will use the title
attribute to identify the pop-up window and then perform operations on it.
How to do it...
We will create a test that retrieves the handles of all the open windows in the current driver context. We will iterate through this list and check the title matching the criteria as follows:
@Test public void testWindowPopupUsingTitle() { //Save the WindowHandle of Parent Browser Window String parentWindowId = driver.getWindowHandle(); //Clicking Visit Us Button will...