Skip to content
CodeKruPro

Search the Page Source

75/80

Search the Page Source

page_source is the HTML the browser has. You can ask whether a string is in it.

Task:

  1. Open Text Fields
  2. Print whether the source contains preFilledTextField
  3. Print whether it contains invisibleField
  4. Print whether the source length is greater than 0

Print the lines below exactly.

Expected output:

Contains preFilledTextField: True
Contains invisibleField: True
Source length > 0: True

About this lesson

Selenium Python page_source: check what HTML the browser holds

Selenium Python AssignmentsAdvancedPage Data

How it works

The driver has a page_source attribute. It returns the current HTML of the page as one Python string. It is an attribute, so there are no parentheses, and calling it as a function raises TypeError: 'str' object is not callable. The text you get is the live document after scripts have run, not the raw file the server sent.

Because the result is an ordinary string, you can use every string tool Python has. The in operator asks whether a piece of text appears anywhere. The len function tells you how long the document is. count tells you how many times a piece of text occurs. The search is case sensitive, so a differently capitalised spelling is not found.

Source search tells you that something is in the markup. It does not tell you that a user can see it. This page has a field that is in the HTML but hidden from view, and the source still contains it. For visibility, find the element and ask whether it is displayed. Source checks are best for broad questions, such as whether an error banner or a tracking snippet was sent. Printing a boolean needs str, or the line fails with TypeError with the message can only concatenate str (not "bool") to str.

About the assignment

Open the Text Fields page and ask three yes or no questions about its HTML: does one id appear, does a second id appear, and is the source not empty. Print each answer as a labelled line.

What you will do

  1. Read the source once and keep it in a variable, so the browser is asked a single time.
  2. Use the in operator for each of the two ids named in the task.
  3. Compare the length of the source with zero.
  4. Wrap each boolean in str and join it to its label before printing.

Common mistakes

  • Calling page_source like a method

    Writing parentheses after it applies them to a string and raises TypeError: 'str' object is not callable. Java users do this by habit after getPageSource().

  • Searching with different capitals

    The check is exact. A search for an id in all lowercase is False even though the element is there.

  • Treating hidden as absent

    The hidden field is present in the source, so the check is True, even though a visitor never sees it. A source search cannot show visibility.

In a real test suite

When a test fails in CI, teams often save page_source next to a screenshot. That file lets you read the page as the browser saw it, long after the container is gone. A pytest hook usually does this on failure.

The next lesson moves to structure: wrapping element reads in your own function.

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

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/TextFields")
    # Search the HTML of the page for the two ids
finally:
    driver.quit()