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
- Read the method in the starter. It currently returns an empty string.
- Inside it, ask
self.driverto find the heading element, using a locator by tag name, CSS or XPath. - Return the element's
text. The method should return it, not print it. - Run the script. The line at the bottom already adds the label.
Common mistakes
Skipping self before find_element
Calling
find_elementon its own raisesNameError: name 'find_element' is not defined. Callingself.find_elementraisesAttributeError, because the class is not a driver. Go throughself.driver.Forgetting to pass the driver
Creating the page with no argument raises a
TypeErrorsaying__init__()is missing thedriverargument.Omitting the call parentheses
Using
page.headingwithout brackets hands back the method itself. The label join then fails with aTypeErrorabout 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.
Related lessons
- Write a Function That Returns Element TextStarts with a plain helper function, the step before a class.
- Locate a Heading with By.TAG_NAMELocates a heading by tag name, which this class method relies on.
- Quit the Browser with a Context ManagerHandles browser cleanup, which sits outside the page class.
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.
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()