An open API service indexing awesome lists of open source software.

https://github.com/lana-20/selenium-browser-windows


https://github.com/lana-20/selenium-browser-windows

Last synced: 8 months ago
JSON representation

Awesome Lists containing this project

README

          

# Browser Windows

__How to Switch between Windows?__

As soon as I open a URL in the browser, the driver focuses only on this particular window. Suppose I click on a link (in the footer). That links opens another browser window. Now I have 2 browser windows. But my driver focus is still on the parent window. The Parent Window is the earlier launched (original) browser window. The other window can be called a Child Window.

If I want to perform any action/operation/activity on the 2nd page (the child browser window), I cannot directly pass. Because the driver is still focused on the main page (the parent browser window).

To perform an action in the Child Window, I have to switch to it. My drver should swttch from one browser window to another, before I am able to to perform the action. Here's where the _switch_to_ command comes into the picture.

When I have 2 browser windows and use the _.close()_ command, it only closes one window at a time, whichever the driver is focused on. But when I use the _.quit()_ command, it closes all the browser windows.

Suppose I have multiple windowns open. I want to close the window(s) of my choice, eg, 1st and 2nd, or 3rd and 4th, or 1st and 5th.

_Interview Question_: How to close a specific browser window based on your choice?

First, I work with switching from one browser window to another (switch between windows).

_.switch_to.window()_ command is available. It requires some input params, such as the browser window ID. I cannot find this window_id in HTML.

driver.switch_to.window(window_id)

Only after I pass in the window_id to the _.switch_to.window()_ command, I can move to that particular browser window.

How do I know the window_id of the browser window? Suppose I have 2 browser windows open. Where can I view the window_id the 1st browser window or the 2nd?

I cannot see window IDs in HTML Window IDs automatically get generated by the browser itself, when I open a browser window. That's the reason why winddow IDs are dynamic. Because at the time of launching my browser and app, my browser auto-generates an ID for that particular window. The ID not depend on the AUT. I do not see the same ID every time. If I have a web site open, and a few seconds later re-open the same URL in another tab, the window ID will be different.

To get that ID, I use the _.current_window_handle_ command:

driver.current_window_handle

This return a window_id of a single browser window at a time. But I have multiple windows open. And I want to get an ID of every open window. For that, I use the _.window_handles_ command:

driver.window_handles

This returns window IDs of multiple open browser windows. Every time I open the AUT in a browser window, randomly a new window_id gets generated by the browser.

Now, I click on a link in the footer. It opens another browser window. I need to get IDs of the 1st and 2nd windows.

driver.window_handles

It returns multiple string values a _list()_ variable type. How to access window ids from this _list()_ variable?

First, return all IDs for the windows that are currently open:

window_ids = driver.window_handles

Then retrieve the Parent Window ID:

window_ids[0]

The following syntax works for 5-10 windows, not too many:

parent_id = windows_ids[0]
child_id = windows_ids[1]

Now I know the IDs of both the Parent and Child Windows. I can switch the driver focus from Parent to Child Window:

driver.switch_to.window(child_id)

As soon as I switch to the Child Window, I need to get the title of the page. I get the page title to know exactly which browser window I've switched to.

driver.title
print(driver.title)

After that, I want to switch to the Parent browser Window:

driver.switch_to.window(parent_id)
print(driver.title)

Suppose that I have 10+ browserr windows. It's difficult to create 10 variables and scale this. It's challenging to differentiate between parents and children and to manage a large number of windows. Here I use the looping approach:

for window in window_ids:
driver.switch_to(window_id)
print(driver.title)

I cannot switch to a window with the page title, only with the window ID.

Now, my requirement is that I have multiple browser windows open and want to close a specific window of choice. Eg, to close the Parent, but keep the Child Window open, use the same loop as above:

for window_id in window_ids:
driver.switch_to.window(window_id)
if driver.title == "OrangeHRM":
driver.close()

Currently I have only 2 windows open. What if I have many and want to close a few of those? Capture the page titles with _driver.title_ and specify the titles to close:

if driver.title == "abc" or driver.title == "xyz":
driver.close()

I can put any number of conditions with the 'or' operator and close any number of windows. As soon as the title conditions match, only those windows close. And the rest stay open. The loop helps achieve it.

for window_id in window_ids:
driver.switch_to.window(window_id)
if driver.title == "abc" or
driver.title == "def" or
...
driver.title == "xyz":
driver.close()

The _.close()_ command closes a single browser at a time. The above command in the loop closes all browser windows, one at a time.

When I want to close specific window(s) of choice, I use the above _if_ statement.