Skip to content
CodeKruPro

Build a One-File Page Object

78/80

Build a One-File Page Object

A page object keeps the locators in methods. This file holds one small class. The method in the starter only returns an empty string.

Task:

  1. Fill in the method so it returns the h2 text
  2. Open Text Fields
  3. Print the heading from the page object

Print the lines below exactly.

Expected output:

Heading: Text Fields

About this lesson

Build a Selenium Python page object class in a single file

Selenium Python AssignmentsAdvancedStructure

How it works

A page object is a class that stands for one page. It holds the driver and offers methods named after what a user does or reads there. Tests call the methods and never touch locators. When the page markup changes, you edit one method, not forty tests.

In Python the setup is short. The __init__ method receives the driver and stores it on self. Each method then reaches the browser through self.driver. Forgetting that prefix is the classic error. A bare driver may even work by accident, because the script has a global of that name, and that hides the bug until the class is moved to another file. A bare find_element fails at once with NameError.

A method that reads something should return it, not print it. Returning lets the caller decide what to do: print, compare or assert. Note that heading here is a method, so the call needs parentheses. Without them you get a bound method object, and joining it to a label raises TypeError with the message can only concatenate str (not "method") to str.

About the assignment

The class and the code that uses it are in the starter. Only the heading method is a placeholder, returning an empty string. Fill it so it gives back the text of the page's main sub-heading.

What you will do

  1. Read the method in the starter. It currently returns an empty string.
  2. Inside it, ask self.driver to find the heading element, using a locator by tag name, CSS or XPath.
  3. Return the element's text. The method should return it, not print it.
  4. Run the script. The line at the bottom already adds the label.

Common mistakes

  • Skipping self before find_element

    Calling find_element on its own raises NameError: name 'find_element' is not defined. Calling self.find_element raises AttributeError, because the class is not a driver. Go through self.driver.

  • Forgetting to pass the driver

    Creating the page with no argument raises a TypeError saying __init__() is missing the driver argument.

  • Omitting the call parentheses

    Using page.heading without brackets hands back the method itself. The label join then fails with a TypeError about a method.

In a real test suite

A suite keeps one page class per screen, often in a pages folder, and a pytest fixture builds the page from the driver fixture. Teams keep assertions in the tests and leave only actions and reads in the page class.

The next lesson uses a plain Python list to repeat dropdown steps.

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
from selenium.webdriver.common.by import By

class TextFieldsPage:
    def __init__(self, driver):
        self.driver = driver

    def heading(self):
        return ""

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextFields")
    page = TextFieldsPage(driver)
    print("Heading: " + page.heading())
finally:
    driver.quit()