Skip to content
CodeKruPro

Set the Window Size

55/80

Set the Window Size

set_window_size asks for a size. get_window_size is what this browser reports afterward. In this headless Chrome those numbers are 1024 and 768.

Task:

  1. Open Buttons
  2. Set the window to 1024 by 768
  3. Print the width and height you read back

Print the lines below exactly.

Expected output:

Width: 1024
Height: 768

About this lesson

Selenium Python set_window_size and get_window_size: set a window to 1024 by 768

Selenium Python AssignmentsAdvancedWindow Management

How it works

A page can lay itself out differently at different widths, so tests often fix the window size first. driver.set_window_size(width, height) takes two integers. Passing one tuple fails with TypeError about a missing positional argument.

driver.get_window_size() returns a plain dictionary with the keys width and height, both integers. A dictionary has no dot access, so size.width raises AttributeError, and a misspelled key raises KeyError. Read values with square brackets. Adding an int to a string raises TypeError, so convert it first.

The numbers describe the outer window. The area where the page renders is smaller. After a window of 800 by 600, JavaScript reported an inner height of 457. For layout checks the inner area matters. Here, the dictionary you read back is the evidence.

Bad input is refused. A negative width raised InvalidArgumentException saying the width is out of range.

About the assignment

Resize the headless Chrome window, then read the size back from the driver instead of printing the numbers you typed.

What you will do

  1. Load the Buttons page.
  2. Set the window to 1024 wide and 768 tall.
  3. Store the dictionary the driver returns for the current size.
  4. Print two labelled lines, pulling width and height from that dictionary by key and converting each to text.

Common mistakes

  • Expecting attribute access on the result

    The size is a dict. size.width raises AttributeError: 'dict' object has no attribute 'width'. Use square brackets and the key.

  • Concatenating a number

    Adding the width to a label raises TypeError with the message can only concatenate str (not "int") to str. Wrap it in str() or use an f-string.

  • Printing the values you set

    Typing the numbers into the print line looks right but proves nothing. Read the size back from the browser.

In a real test suite

Suites set the window once, inside a driver fixture in conftest.py, so every test starts with the same dimensions. Responsive checks are often parametrized over a few widths.

Size can decide whether a test passes at all. Lesson 59 needs a larger window before a drag lands.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Buttons")
    # Set 1024 by 768 and print the size you read back
finally:
    driver.quit()