Skip to content
CodeKruPro

Write a Function That Returns Element Text

76/80

Write a Function That Returns Element Text

A function can take an element and return its text. The rest of the script calls that function.

Task:

  1. Write a function that returns element.text
  2. Open Text Fields
  3. Print the h2 text using the function

Print the lines below exactly.

Expected output:

Heading: Text Fields

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

  1. Read the helper in the starter: it takes one argument, an element.
  2. Replace the placeholder so the function returns the element's text attribute.
  3. 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 with TypeError with the message can only concatenate str (not "NoneType") to str.

  • Adding parentheses to text

    The text property is already a string. Calling it raises TypeError: '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.

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

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()