Print Statements

Introduction

While modern IDEs provide powerful debugging tools such as breakpoints, variable inspection, and browser-side debugging, sometimes the simplest debugging technique is also one of the most useful. Python’s built-in print() function allows developers to display valuable runtime information directly in the terminal during test execution.

Print statements are particularly useful for quickly understanding:

  • Current browser information.

  • Variable values.

  • Element properties.

  • Application state changes.

  • Test execution flow.

  • Assertion failures.

Although print statements should not replace proper debugging tools entirely, they are extremely helpful during automation development and troubleshooting.

In this tutorial, you will learn how print statements work in Selenium automation, understand when they should be used, explore practical examples, common mistakes, best practices, and frequently asked interview questions.


What are Print Statements?

Print statements display useful information during program execution that helps developers understand what is happening at various stages of the automation script.

Instead of:

Test Fails
     │
     ▼
No Information Available
     │
     ▼
Investigate Manually
     │
     ▼
Repeat Multiple Times

we can use print statements:

Test Executes
      │
      ▼
Display Runtime Information
      │
      ▼
Inspect Variable Values
      │
      ▼
Understand Test Behavior
      │
      ▼
Identify the Problem Easily

Print statements significantly simplify quick debugging activities.


Why Should We Use Print Statements?

Print statements allow developers to:

  • Display variable values.

  • Verify browser information.

  • Inspect Selenium WebElements.

  • Understand execution flow.

  • Troubleshoot failed assertions.

  • Perform quick debugging.

Large automation frameworks frequently use logging mechanisms for production environments, but print statements remain extremely useful during development and learning.


Practical Example

The following example demonstrates how to use print statements while debugging Selenium automation scripts.

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: Print Statements
# Practice site:
# https://www.testmuai.com/selenium-playground/simple-form-demo
# Run:
# pytest -s 63_examples/test_05_print_statements.py
#
# Print statements provide a simple and effective way to display useful
# debugging information while Selenium tests are executing.


def test_print_statements():

    driver = webdriver.Chrome()

    try:
        driver.get(
            "https://www.testmuai.com/"
            "selenium-playground/simple-form-demo"
        )

        print(
            f"Current URL: "
            f"{driver.current_url}"
        )

        print(
            f"Page title: "
            f"{driver.title}"
        )

        message_input = driver.find_element(
            By.ID,
            "user-message"
        )

        message_input.send_keys(
            "Print Debug"
        )

        print(
            f"Typed value: "
            f"{message_input.get_attribute('value')}"
        )

        driver.find_element(
            By.ID,
            "showInput"
        ).click()

        output = driver.find_element(
            By.ID,
            "message"
        ).text

        print(
            f"Displayed message: "
            f"{output}"
        )

        assert output == "Print Debug"

    finally:
        driver.quit()

Output

Current URL:
https://www.testmuai.com/
selenium-playground/simple-form-demo

Page title:
Selenium Playground

Typed value:
Print Debug

Displayed message:
Print Debug

Assertions Passed.

Test Executed Successfully.

Note: The actual output may vary if the webpage content changes in future versions of the application.


Understanding the Code

Import Required Modules

from selenium import webdriver

from selenium.webdriver.common.by import By

Imports:

  • Selenium WebDriver.

  • Locator strategies.


Launch Chrome Browser

driver = webdriver.Chrome()

Creates a new Chrome browser session.


Open the Website

driver.get(
    "https://www.testmuai.com/"
    "selenium-playground/simple-form-demo"
)

Opens the Selenium Playground webpage.


Display the Current URL

print(
    f"Current URL: "
    f"{driver.current_url}"
)

Example output:

Current URL:

https://www.testmuai.com/
selenium-playground/simple-form-demo

Displaying the current URL is often useful while debugging navigation-related problems.


Display the Page Title

print(
    f"Page title: "
    f"{driver.title}"
)

Example output:

Page title:

Selenium Playground

This helps verify whether Selenium successfully loaded the expected webpage.


Enter User Input

message_input.send_keys(
    "Print Debug"
)

The following statement retrieves the value currently stored inside the input field.

print(
    f"Typed value: "
    f"{message_input.get_attribute('value')}"
)

Output:

Typed value:

Print Debug

This is extremely useful while debugging form-related automation scripts.


Display the Updated Message

print(
    f"Displayed message: "
    f"{output}"
)

Output:

Displayed message:

Print Debug

Displaying runtime information significantly simplifies troubleshooting efforts.


Verify the Result

assert output == "Print Debug"

The assertion verifies that:

  • The message was entered successfully.

  • The displayed value matches the expected result.


Close the Browser

driver.quit()

Closes all browser windows and properly ends the WebDriver session.


Running the Example

Execute the following command:

py -3 -m pytest -s ^
"63_examples/test_05_print_statements.py"

Run all debugging examples together:

py -3 -m pytest -s ^
"63_examples/"

Note: The -s option is extremely important because PyTest normally captures console output. Using -s allows all print statements to be displayed directly in the terminal during execution.


Why is -s Required?

Without:

pytest -s

PyTest captures the output internally.

Print Statement
       │
       ▼
PyTest Captures Output
       │
       ▼
Nothing Displayed

With:

pytest -s

the execution becomes:

Print Statement
       │
       ▼
Display Output
       │
       ▼
Terminal Window
       │
       ▼
Easy Debugging

Execution Flow

Launch Browser
       │
       ▼
Open Website
       │
       ▼
Display Runtime Information
       │
       ▼
Perform User Actions
       │
       ▼
Display Updated Values
       │
       ▼
Verify Results
       │
       ▼
Assertions Passed
       │
       ▼
Close Browser

Automation Testing Example

Print statements are useful while debugging:

print(driver.title)

print(driver.current_url)

print(element.text)

print(element.is_displayed())

print(element.get_attribute("value"))

These statements provide immediate feedback during automation execution without requiring additional debugging tools.


Real-World Example

Large automation frameworks frequently display:

  • Current URLs.

  • Browser titles.

  • Test data values.

  • Element states.

  • Execution progress.

  • Failure information.

For example:

Test Started
      │
      ▼
Display Current URL
      │
      ▼
Display User Input
      │
      ▼
Display Application Output
      │
      ▼
Verify Assertions
      │
      ▼
Test Completed Successfully

Print statements are extremely useful during framework development and troubleshooting activities.


Common Mistakes Beginners Make

Forgetting to Use -s

Incorrect

pytest test_file.py

The print statements may not appear in the terminal.


Correct

pytest -s test_file.py

Always use -s whenever console output is required.


Printing Excessive Information

Avoid writing:

print(driver.page_source)

unless absolutely necessary.

Displaying extremely large amounts of information can make debugging more difficult.

Prefer displaying only meaningful information whenever possible.


Using Print Statements Everywhere

Print statements are useful for:

  • Quick debugging.

  • Learning Selenium.

  • Understanding execution flow.

For larger automation frameworks, consider using:

  • Logging mechanisms.

  • Reporting tools.

  • Debuggers.

  • Screenshots.


Best Practices

  • Use pytest -s whenever print statements are required.

  • Display only useful debugging information.

  • Prefer meaningful output messages.

  • Combine print statements with assertions whenever appropriate.

  • Use logging mechanisms for larger automation frameworks.

  • Avoid printing unnecessary information.

  • Utilize print statements strategically during troubleshooting.


Conclusion

Print statements remain one of the simplest and most effective debugging techniques available in Selenium automation testing. They provide immediate visibility into variable values, browser information, and application behavior during test execution.

Although modern debugging tools offer more advanced capabilities, print statements continue to play an important role during automation development, learning, and troubleshooting activities.

Mastering simple debugging techniques such as print statements significantly improves productivity while developing Selenium automation frameworks.


Frequently Asked Questions (FAQs)

What are print statements used for in Selenium?

Print statements are used to display useful runtime information such as:

  • URLs.

  • Variable values.

  • Element properties.

  • Application outputs.

  • Test execution details.


Why should I use pytest -s?

The -s option prevents PyTest from capturing console output and allows print statements to appear directly in the terminal.


Should I always use print statements for debugging?

Print statements are excellent for quick debugging. However, larger automation frameworks commonly use logging mechanisms and dedicated debugging tools alongside print statements.


Can print statements help identify failed assertions?

Yes.

Displaying actual and expected values before assertions greatly simplifies troubleshooting efforts.


Are print statements still useful with modern IDE debuggers?

Yes.

Print statements provide immediate feedback and are often faster for simple debugging tasks.


Key Takeaways

  • Print statements provide a simple and effective debugging mechanism for Selenium automation testing.

  • The -s option is required to display console output while running PyTest tests.

  • Print statements are particularly useful for displaying browser information and variable values.

  • Meaningful output messages significantly simplify troubleshooting efforts.

  • Logging mechanisms should be preferred for larger automation frameworks whenever appropriate.

  • Print statements complement modern debugging tools rather than replace them.

  • Proper debugging practices substantially improve framework maintainability.

  • Print Statements are an important Selenium automation and interview topic.