About this lesson
Write a Selenium Python helper function that returns element text
Selenium Python AssignmentsAdvancedStructure
How it works
A function lets you name a step and reuse it. In Python you start it with def, give it a name and parameters, and send a result back with return. Without return the function gives back None, and the caller gets nothing useful. This is the most common slip when learners first write helpers.
Here the parameter is an element, not a locator. The caller finds the element, hands it over, and the function reads its text. That is a small design choice with a reason: the function does not need to know how the element was found, so one helper serves many pages. Other helpers take a locator pair and do the search too, which is handy but ties the helper to the browser.
Take care with text. It is a property, so element.text has no parentheses. Writing element.text() raises TypeError: 'str' object is not callable. The result is a string, so you can join it to a label with +. If your function prints and does not return, the join fails with TypeError with the message can only concatenate str (not "NoneType") to str.
About the assignment
The starter already finds the heading and prints a label followed by the helper's result. The helper body is a placeholder that returns an empty string. Make it return the real visible text of the element it is given.
What you will do
- Read the helper in the starter: it takes one argument, an element.
- Replace the placeholder so the function returns the element's
textattribute. - Run the script and compare the printed line with the expected output. If the label shows with nothing after it, the helper still returns the old value.
Common mistakes
Printing inside the helper
A function that prints but never returns gives back
None. The label join then fails withTypeErrorwith the messagecan only concatenate str (not "NoneType") to str.Adding parentheses to text
The
textproperty is already a string. Calling it raisesTypeError: 'str' object is not callable.Defining the helper but never changing it
The starter helper returns an empty string on purpose. The script runs without any error, and the heading line just comes out blank.
In a real test suite
In a real suite these helpers live in a utility module or in conftest.py, and tests import them. A good helper hides a wait, a retry or a trim of the text, so no test repeats that logic.
The next lesson makes sure the browser is closed even when a line in the block fails, with a with statement.
Related lessons
- Read Visible TextTeaches the
textproperty that this helper wraps. - Locate a Heading with By.TAG_NAMEFinds a heading by tag name, the same way the starter does.
- Build a One-File Page ObjectMoves helper methods into a class, the next step in structure.
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
def element_text(element):
# Return the visible text.
return ""
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Elements/TextFields")
heading = driver.find_element(By.TAG_NAME, "h2")
print("Heading: " + element_text(heading))
finally:
driver.quit()