0
0
Selenium Pythontesting~5 mins

Element locators in page class in Selenium Python

Choose your learning style9 modes available
Introduction

We use element locators in a page class to find parts of a web page easily and clearly. This helps us write tests that are simple and organized.

When you want to click a button on a web page during a test.
When you need to check if a text box is visible before typing.
When you want to get the text from a label or heading.
When you want to reuse the same web element in many tests without repeating code.
When you want to keep your test code clean and easy to update if the page changes.
Syntax
Selenium Python
class PageClassName:
    def __init__(self, driver):
        self.driver = driver
        self.element_name = (By.LOCATOR_TYPE, 'locator_value')

Use tuples with By to define locators clearly.

Keep locators as instance variables to reuse them easily.

Examples
This example shows locators by ID, NAME, and CSS selector in a login page class.
Selenium Python
from selenium.webdriver.common.by import By

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_input = (By.ID, 'username')
        self.password_input = (By.NAME, 'password')
        self.login_button = (By.CSS_SELECTOR, 'button.login')
Here, locators use XPath and class name to find elements on the home page.
Selenium Python
from selenium.webdriver.common.by import By

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        self.search_box = (By.XPATH, '//input[@type="search"]')
        self.submit_button = (By.CLASS_NAME, 'submit-btn')
Sample Program

This test script uses a page class with element locators to enter username and password, then click login. It prints a success message if no errors occur.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_input = (By.ID, 'username')
        self.password_input = (By.NAME, 'password')
        self.login_button = (By.CSS_SELECTOR, 'button.radius')

    def enter_username(self, username):
        self.driver.find_element(*self.username_input).send_keys(username)

    def enter_password(self, password):
        self.driver.find_element(*self.password_input).send_keys(password)

    def click_login(self):
        self.driver.find_element(*self.login_button).click()

# Setup Chrome driver (assuming chromedriver is in PATH)
options = Options()
options.add_argument('--headless')  # Run browser in headless mode
service = Service()
driver = webdriver.Chrome(service=service, options=options)

try:
    driver.get('https://the-internet.herokuapp.com/login')
    login_page = LoginPage(driver)
    login_page.enter_username('testuser')
    login_page.enter_password('password123')
    login_page.click_login()
    print('Test passed: Login actions performed successfully')
except Exception as e:
    print(f'Test failed: {e}')
finally:
    driver.quit()
OutputSuccess
Important Notes

Use the * operator to unpack locator tuples when calling find_element.

Keep locators in one place to make maintenance easier if the page changes.

Use meaningful variable names for locators to understand what element they represent.

Summary

Element locators in a page class help find web elements clearly and reuse them.

Locators are stored as tuples with By strategies and values.

This approach keeps test code clean, simple, and easy to maintain.