window handles selenium что это

How to handle multiple windows in Selenium?

Software testing plays a vital role in creating error-free products that offer seamless user experiences. Therefore, it is essential to test the software before putting it into production.

As technology advances, it is necessary to adopt automation testing into the development pipeline. Selenium is the most widely used framework for automated software testing of a website. Every website must be tested by putting it through multiple real-world user scenarios. One such scenario is the handling of multiple windows. This article will discuss how to automate this action with Selenium so that the website’s behavior can be monitored by testers.

Handling Multiple Windows in Selenium

The user scenario being automated here is: Open a new tab and then switch back to the last window to complete the other pending activities. In such scenarios, Selenium helps to handle multiple windows through window handlers and javascript executors.

What is a window handle?

It is a unique identifier that holds the address of all the windows. Think of it as a pointer to a window, which returns the string value. It is assumed that each browser will have a unique window handle. This window handle function helps to retrieve the handles of all windows.

Syntax

These are some of the methods that will be used to handle multiple windows in Selenium.

Example of handling multiple windows

Scenario: Navigate to the Browserstack home page. This is the parent window. From the parent window, let’s see how to handle the child windows and then again navigate back to the parent windows.

Refer to the complete program below.

Before running the code, one should do a quick check on 6 things to avoid while running selenium scrips. Check it out.

Output:

On executing the parent window handle, it will open multiple child windows and then navigate back to the final window handle.

Now let’s perform some actions on the Browserstack website.

Pro Tip: Want to dive deeper into Selenium implementation on BrowserStack with free interactive courses and lab exercises? Visit Test University


Code Snippet

On executing the code above, it will launch multiple windows and the window handle will be retrieved. Run the code, automate user navigation through multiple windows, and ensure the website works perfectly in real user conditions. With Selenium WebDriver, ensure that websites offer an optimal user experience in all possible circumstances.

Источник

How to handle multiple windows in Selenium?

While automating any website or a web application, we must have witnessed a scenario where multiple windows open within an application when a button is clicked and the user has to perform some action on all the opened windows. Now user might not be able to work on all windows at the same time and hence need some mechanism through which he can take control over parent and child windows or if I may say from a QA’s perspective, how can we handle window using Selenium?. In this tutorial, we will be understanding how this can be achieved with Selenium WebDriver. We will be focusing on the following key topics:

What is a window in Selenium?

A window in any browser is the main webpage on which the user is landed after hitting a link/URL. Such a window in Selenium is referred to as the parent window also known as the main window which opens when the Selenium WebDriver session is created and has all the focus of the WebDriver.

To view an example of how the main window looks like you can visit the ToolsQA demo site, and check. The same is shown in the screenshot below:

As you can see in the image above, even though the web page has multiple elements, but all of them are part of the main window. The URL navigated using the Selenium WebDriver will always have the context of the main window. But when we click on the buttons «New Window « and «New Window Message «, it will open other windows, within the parent window. These windows are Child Windows. Let’s understand how to locate the parent and child windows?

How do we identify parent window and child windows?

When a user hits a URL, a webpage opens. This main page is the parent window i.e the main window on which the user has currently landed and will perform any operation. This is the same webpage that will open when our Selenium automation script will execute. All the windows which will open inside your main window will be termed as child windows.

Taking an example of ToolsQA demo site, the main page which has elements like the new tab, new window, etc is our parent window and the two sub-windows displayed are the child windows that will open when we click on «New Window» and «New Window Message» buttons, as shown below:

Note: There can be a single child window or multiple child windows in your parent window.

A child window may or may not have any URL. As shown above, Child Window 1 doesn’t have any explicit URL, whereas Child Window 2 has an explicit URL.

So, when we are testing a web application manually, it is very easy to check the behavior of child windows, as they are easily visible in the context of the main window. But the same is not the case while automating using Selenium. Let’s understand what is the need to handle the different window types when automating the application using Selenium WebDriver?

Why do we need to handle multiple windows in Selenium?

When a user is working on a web application, there might arise a scenario where a new window will open inside your main window. Consider a scenario of an e-commerce site selling garments that will have a size chart linked against each garment and upon clicking, a child window opens. Now, as we understand that Selenium works in a specific context only, and each of the child windows will have a separate context. So, for automating such scenarios using Selenium WebDriver, we have to direct the WebDriver accordingly, so as Selenium can get the context of the specific window and perform the needed actions in that window.

The below code-snippet will click on the «New Window « button and will try to read the text from the new Window.

Output : Once we execute the code, an «no such element: Unable to locate element « error is encountered as the WebDriver is not able to locate the element and get the text which is displayed on the new window.

As we can see, even though the text was visible on the screen, but Selenium WebDriver was not able to locate it, as it was not in the same context in which Selenium WebDriver was executing.

For switching the context, Selenium WebDriver makes use of a specific ID of the window, known as the handle of the window. Let’s understand what exactly is a window handle in the Selenium context?

What is a window handle in Selenium?

A window handle stores the unique address of the browser windows. It is just a pointer to a window, whose return type is alphanumeric. The window handle in Selenium helps in handling multiple windows and child windows. Each browser will have a unique window handle value with which we can uniquely identify it.

Читайте также:  асино лето чему учит

Each window here will have a unique ID that we can get using the methods provided by Selenium Webdriver and then use the same to switch the context to that specified window. Let’s first understand what are the different methods provided by Selenium WebDriver for handling windows?

What are the different methods used for window handling in Selenium?

Selenium WebDriver provides various methods for handling of windows. Few of them are:

How do we handle child windows in Selenium?

As seen in the above example if we have child windows in any web application then interaction with them without proper window handling will lead to an exception. For this we have different methods explained above, we will use them here with a practical example.

We will be using getWindowHandle( ) and getWindowHandles( ) method here along with switchto() method. In the snapshot below, we have highlighted the two main methods for window handling in Selenium.

Taking the same example of » ToolsQA Demo Site» above where exception was encountered, we will show how it will be executed successfully. After opening the URL, we will click on the «New window» button within the application, a new browser window opens. We will read the text from the newly opened window i.e » This is sample page» and will print it.

The output of the code will print the text “This is a sample page ” as shown below:

So this way, we switched the context to the child window and then printed the text in the child windows.

How to handle multiple windows in Selenium?

In Selenium, when we have multiple windows in any web application, the approach may need to switch control among several windows i.e from one window to another to perform any action and we can achieve this by using switchto(); method. Furthermore, we will be using window handle here to store the unique value of the windows and perform window handling using Selenium.

Note: If you have to switch between tabs then also you have to use the same approach.

Let’s understand this with the help of the code below:

Once the Selenium WebDriver instantiates, allocation of a unique alphanumeric id happens to the window called window handle and it identifies the browser windows. In the above code, parent window and one of the child window has the same ID, the other two windows have a different ID. This is because a parent window is the child of itself. But notice that only two closed due to the same reason. Since this id is distinctive, the Selenium WebDriver use such it to switch between different windows (or tabs). The id retains until the session closes.

How do we switch back to the parent window from the child windows in Selenium?

Once you have switched to the child window, the Selenium WebDriver will hold the current context of it, and you will not be able to identify elements present in the parent window. To access the elements of the parent window we have to shift the focus back on it. We can achieve this as shown in the below code snippet:

The output of the above code is shown below:

Practise Exercise : Try to implement the following scenario, where you can print text from the main window after switching back to the parent window.

How to close all windows in Selenium?

When we are working on multiple windows it is important to close windows simultaneously when we finish the action. For closing the window on which WebDriver has current focus we have driver.close(); method. We use this method majorly when we have multiple windows and we wish to close a selective window.

Another method that exists for the closing window is driver.quit( ) which will close all the windows opened in a particular session. It basically stops the driver instance and any further actions to WebDriver may result in an exception. It is generally the last statement of any code.

In the examples given above, we have used both methods.

Note: After closing a child window we have to explicitly switch back to the parent window before using any WebDriver command to avoid «nosuchwindow» exception.

Источник

Handling multiple windows in Python Selenium

The Blog To Learn Selenium and Test Automation

Handling multiple windows in Python Selenium

There are many cases where handling multiple windows while working with a web application is required, either application navigates to or opens multiple windows and user has to perform operations in this new window. Those are may be advertisements or kind of information showing on popup windows such as terms & conditions, privacy policy or kind of web page itself where user has to enter information.

Python Selenium provides option to handle multiple windows using ‘window_handles’. Python Selenium WebDriver assigns an id to each window as soon as the WebDriver object is instantiated or new window is opened using a WebDriver object. This unique id is known as window handles.

Also note that WebDriver object always controls only one window at a time in the current session. For example, opening a link in new window does not transfer control of WebDriver to new window. WebDriver will be still controlling the old window and any operations that we perform using Selenium script will be forwarded to this old window.

We can use this unique id to differentiate a window and switch control among multiple windows.

Python provides two in-built objects

window_handles

Returns the handles of all windows within the current session.

Syntax: driver.window_handles

current_window_handle

Returns the handle of the current window.

Syntax: driver.current_window_handle

Example 1:
For example, to print title of all windows in the current session

To print title of multiple windows opened, we can follow below steps.
1. Get all window handles
2. Switch to the window using driver.switch_to.window(handles)
3. Get and print window title

Assume that driver.find_element_by_id(“link”).click(); will open up a new window in current session.

Example 2:
For example, User wants to print title of all windows except current window.

We can follow below steps to print title of new window among multiple windows.

1. Get all window handles
2. Get current window handle
3. If handle is not current window handle, Switch to the window using driver.switch_to.window(handles)
4. Get and print window title

Читайте также:  windows tiff ifilter что это

Assume that driver.find_element_by_id(“link”).click(); will open up a new window in current session.

Example 3:
For example, User wants to do some operation in newly opened child window, close it after all operations and do some actions in parent window.

We can follow below steps to perform this multiple windows operation.

1. Get all window handles
2. Get parent window handle and store in a temp variable say ‘parent_handle’
3. If handle is not parent window handle, Switch to the child or new window using driver.switch_to.window(handles)
4. Perform all required operations and close the child or new window
5. Shift the control back to parent window
6. Perform required operations

Assume that driver.find_element_by_id(“link”).click(); will open up a new window in current session.

Let’s see in another post, how to handle new/multiple tabs in same browser window.

Happy coding. Please let us know your thoughts in comments section.

Источник

3. NavigatingВ¶

The first thing you’ll want to do with WebDriver is navigate to a link. The normal way to do this is by calling get method:

3.1. Interacting with the pageВ¶

Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways to find elements. For example, given an element defined as:

you could find it using any of:

WebDriver has an “Object-based” API; we represent all types of elements using the same interface. This means that although you may see a lot of possible methods you could invoke when you hit your IDE’s auto-complete key combination, not all of them will make sense or be valid. Don’t worry! WebDriver will attempt to do the Right Thing, and if you call a method that makes no sense (“setSelected()” on a “meta” tag, for example) an exception will be raised.

So, you’ve got an element. What can you do with it? First of all, you may want to enter some text into a text field:

You can simulate pressing the arrow keys by using the “Keys” class:

It is possible to call send_keys on any element, which makes it possible to test keyboard shortcuts such as those used on GMail. A side-effect of this is that typing something into a text field won’t automatically clear it. Instead, what you type will be appended to what’s already there. You can easily clear the contents of a text field or textarea with the clear method:

3.2. Filling in formsВ¶

We’ve already seen how to enter text into a textarea or text field, but what about the other elements? You can “toggle” the state of the drop down, and you can use “setSelected” to set something like an OPTION tag selected. Dealing with SELECT tags isn’t too bad:

This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn.

As you can see, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called a “Select”, which provides useful methods for interacting with these:

WebDriver also provides features for deselecting all the selected options:

This will deselect all OPTIONs from that particular SELECT on the page.

Suppose in a test, we need the list of all default selected options, Select class provides a property method that returns a list:

To get all available options:

Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:

Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised:

3.3. Drag and dropВ¶

You can use drag and drop, either moving an element by a certain amount, or on to another element:

3.4. Moving between windows and framesВ¶

It’s rare for a modern web application not to have any frames or to be constrained to a single window. WebDriver supports moving between named windows using the “switch_to_window” method:

All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:

Alternatively, you can pass a “window handle” to the “switch_to_window()” method. Knowing this, it’s possible to iterate over every open window like so:

You can also swing from frame to frame (or into iframes):

It’s possible to access subframes by separating the path with a dot, and you can specify the frame by its index too. That is:

would go to the frame named “child” of the first subframe of the frame called “frameName”. All frames are evaluated as if from *top*.

Once we are done with working on frames, we will have to come back to the parent frame which can be done using:

3.5. Popup dialogsВ¶

Selenium WebDriver has built-in support for handling popup dialog boxes. After you’ve triggered action that would open a popup, you can access the alert with the following:

This will return the currently open alert object. With this object, you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, prompts. Refer to the API documentation for more information.

3.6. Navigation: history and locationВ¶

Earlier, we covered navigating to a page using the “get” command ( driver.get(«http://www.example.com») ). As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task. To navigate to a page, you can use get method:

To move backward and forward in your browser’s history:

Please be aware that this functionality depends entirely on the underlying driver. It’s just possible that something unexpected may happen when you call these methods if you’re used to the behavior of one browser over another.

3.7. CookiesВ¶

Before moving to the next section of the tutorial, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for:

Источник

Browser manipulation

Ruby is not installed by default on Windows. Download the latest version and run the installer. You can leave all settings at default values, except at the Installation Destination and Optional Tasks screen check where you need to check the Add Ruby executables to your PATH checkbox. To drive any browser, you have to install selenium-webdriver Ruby gem. To install it, open command prompt and type this:

Читайте также:  беспочвенная тревога что это

Or, if you use Bundler, add this line to your application’s Gemfile:

And then execute the following command in command prompt:

Internet Explorer

C:\Ruby200\bin looks like a good place. Unzip IEDriverServer file and move IEDriverServer.exe there.

This should open a new Internet Explorer window:

Browser navigation

Navigate to

The first thing you will want to do after launching a browser is to open your website. This can be achieved in a single line:

Get current URL

You can read the current URL from the browser’s address bar using:

Pressing the browser’s back button:

Forward

Pressing the browser’s forward button:

Refresh

Refresh the current page:

Get title

You can read the current page title from the browser:

Windows and tabs

Get window handle

WebDriver does not make the distinction between windows and tabs. If your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using:

Switching windows or tabs

Clicking a link which opens in a new window will focus the new window or tab on screen, but WebDriver will not know which window the Operating System considers active. To work with the new window you will need to switch to it. If you have only two tabs or windows open, and you know which window you start with, by the process of elimination you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original.

However, Selenium 4 provides a new api NewWindow which creates a new tab (or) new window and automatically switches to it.

Create new window (or) new tab and switch

Creates a new window (or) tab and will focus the new window or tab on screen. You don’t need to switch to work with the new window (or) tab. If you have more than two windows (or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original.

Note: This feature works with Selenium 4 and later versions.

Closing a window or tab

When you are finished with a window or tab and it is not the last window or tab open in your browser, you should close it and switch back to the window you were using previously. Assuming you followed the code sample in the previous section you will have the previous window handle stored in a variable. Put this together and you will get:

Forgetting to switch back to another window handle after closing a window will leave WebDriver executing on the now closed page, and will trigger a No Such Window Exception. You must switch back to a valid window handle in order to continue execution.

Quitting the browser at the end of a session

When you are finished with the browser session you should call quit, instead of close:

Failure to call quit will leave extra background processes and ports running on your machine which could cause you problems later.

Some test frameworks offer methods and annotations which you can hook into to tear down at the end of a test.

If not running WebDriver in a test context, you may consider using try / finally which is offered by most languages so that an exception will still clean up the WebDriver session.

Python’s WebDriver now supports the python context manager, which when using the with keyword can automatically quit the driver at the end of execution.

Frames and Iframes

Frames are a now deprecated means of building a site layout from multiple documents on the same domain. You are unlikely to work with them unless you are working with an pre HTML5 webapp. Iframes allow the insertion of a document from an entirely different domain, and are still commonly used.

If you need to work with frames or iframes, WebDriver allows you to work with them in the same way. Consider a button within an iframe. If we inspect the element using the browser development tools, we might see the following:

If it was not for the iframe we would expect to click on the button using something like:

However, if there are no buttons outside of the iframe, you might instead get a no such element error. This happens because Selenium is only aware of the elements in the top level document. To interact with the button, we will need to first switch to the frame, in a similar way to how we switch windows. WebDriver offers three ways of switching to a frame.

Using a WebElement

Switching using a WebElement is the most flexible option. You can find the frame using your preferred selector and switch to it.

Using a name or ID

If your frame or iframe has an id or name attribute, this can be used instead. If the name or ID is not unique on the page, then the first one found will be switched to.

Using an index

It is also possible to use the index of the frame, such as can be queried using window.frames in JavaScript.

Leaving a frame

To leave an iframe or frameset, switch back to the default content like so:

Window management

Screen resolution can impact how your web application renders, so WebDriver provides mechanisms for moving and resizing the browser window.

Get window size

Fetches the size of the browser window in pixels.

Set window size

Get window position

Fetches the coordinates of the top left coordinate of the browser window.

Set window position

Moves the window to the chosen position.

Maximize window

Enlarges the window. For most operating systems, the window will fill the screen, without blocking the operating system’s own menus and toolbars.

Minimize window

Minimizes the window of current browsing context. The exact behavior of this command is specific to individual window managers.

Minimize Window typically hides the window in the system tray.

Note: This feature works with Selenium 4 and later versions.

Fullscreen window

Fills the entire screen, similar to pressing F11 in most browsers.

TakeScreenshot

Used to capture screenshot for current browsing context. The WebDriver endpoint screenshot returns screenshot which is encoded in Base64 format.

TakeElementScreenshot

Used to capture screenshot of an element for current browsing context. The WebDriver endpoint screenshot returns screenshot which is encoded in Base64 format.

Execute Script

Executes JavaScript code snippet in the current context of a selected frame or window.

Print Page

Prints the current page within the browser.

Note: This requires Chromium Browsers to be in headless mode

Selenium Level Sponsors

Support the Selenium Project

Want to support the Selenium project? Learn more or view the full list of sponsors.

Источник

Информ портал о технике и не только