selenium-webdriver Interacting with the Browser Window(s) Managing the active window


Example

C#

Maximizing the window

driver.Manage().Window.Maximize();

This is fairly straightforward, ensures that our currently active window is maximized.

Position of the window

driver.Manage().Window.Position = new System.Drawing.Point(1, 1);

Here we essentially move the currently active window to a new position. In the Point object we provide x and y co-ordinates; these are then used as offsets from the top-left corner of the screen to determine where the window should be placed. Note that you can also store the window position in a variable:

System.Drawing.Point windowPosition = driver.Manage().Window.Position;

Size of the window

Setting and getting the window size uses the same syntax as the position:

driver.Manage().Window.Size = new System.Drawing.Size(100, 200);
System.Drawing.Size windowSize = driver.Manage().Window.Size;

URL of the window

We can obtain the current URL of the active window:

string url = driver.Url;

We can also set the URL for the active window, which will make the driver navigate to the new value:

driver.Url = "http://stackoverflow.com/";

Window handles

We can obtain the handle for the current window:

string handle = driver.CurrentWindowHandle;

And we can obtain the handles for all open windows:

IList<String> handles = driver.WindowHandles;

Python

Maximizing the window

driver.maximize_window()

Get position of the window

driver.get_window_position() # returns {'y', 'x'} coordinates

Set position of the window

driver.set_window_position(x, y) # pass 'x' and 'y' coordinates as arguments

Get size of the window

driver.get_window_size() # returns {'width', 'height'} values

Set size of the window

driver.set_window_size(width, height) # pass 'width' and 'height' values as arguments

Current page title

driver.title

Current URL

driver.current_url

Window handles

driver.current_window_handle

List of currently opened windows

driver.window_handles