Introduction
Developing Selenium automation scripts often involves troubleshooting issues such as failed assertions, incorrect locators, synchronization problems, and unexpected browser behavior. While print statements can provide useful information, modern IDEs such as PyCharm offer powerful debugging capabilities that make troubleshooting significantly easier.
PyCharm allows automation engineers to pause test execution, inspect variables, evaluate expressions, analyze the call stack, and execute Selenium tests step by step. These features greatly simplify identifying and resolving automation failures.
Instead of repeatedly modifying your test scripts to determine what went wrong, PyCharm allows you to inspect the application’s state during execution and understand Selenium’s behavior in real time.
In this tutorial, you will learn how to debug Selenium tests in PyCharm, understand breakpoints and debugger tools, explore practical examples, common mistakes, best practices, and frequently asked interview questions.
What is Debugging in PyCharm?
Debugging is the process of temporarily pausing program execution and examining its current state to identify problems within the code.
Instead of:
Test Fails
│
▼
Add Print Statements
│
▼
Run Again
│
▼
Failure Continues
│
▼
Repeat Multiple Times
PyCharm allows us to:
Test Starts
│
▼
Breakpoint Reached
│
▼
Execution Paused
│
▼
Inspect Variables
│
▼
Evaluate Expressions
│
▼
Continue Execution
│
▼
Identify the Problem Easily
Debugging significantly improves productivity while developing Selenium automation frameworks.
Why Should We Use PyCharm for Debugging?
PyCharm provides several useful debugging features, including:
Breakpoints.
Variable inspection.
Evaluate Expression.
Call stack analysis.
Step-by-step execution.
Integrated PyTest support.
Watch expressions.
Debug configurations.
These features greatly simplify Selenium test debugging.
Practical Example
The following example demonstrates how to debug a Selenium test in PyCharm using breakpoints and the built-in Python debugger.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: Debugging in PyCharm
# Practice site:
# https://www.testmuai.com/selenium-playground/checkbox-demo
# Run:
# pytest -s 63_examples/test_02_debugging_in_pycharm.py
#
# Set breakpoints in the gutter and run the test using PyCharm's debugger to
# inspect variables and observe Selenium's behavior during execution.
def test_debugging_in_pycharm():
driver = webdriver.Chrome()
try:
driver.get(
"https://www.testmuai.com/"
"selenium-playground/checkbox-demo"
)
# Set a breakpoint here.
checkbox = driver.find_element(
By.ID,
"isAgeSelected"
)
checkbox.click()
assert checkbox.is_selected()
finally:
driver.quit()
Output
Chrome browser launched successfully.
Website opened successfully.
Execution paused at the breakpoint.
Variables inspected successfully.
Execution resumed successfully.
Assertions Passed.
Test Executed Successfully.
Note: When running the test in Debug mode, PyCharm pauses execution whenever a breakpoint is encountered and waits for user interaction.
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/checkbox-demo"
)
Opens the Selenium Playground webpage.
Set the Breakpoint
checkbox = driver.find_element(
By.ID,
"isAgeSelected"
)
This is the ideal location for placing a breakpoint.
When execution reaches this line:
Test Starts
│
▼
Breakpoint Reached
│
▼
Execution Paused
│
▼
Inspect Variables
│
▼
Continue Execution
PyCharm automatically pauses the test and opens various debugging tools.
How to Set Breakpoints?
Click beside the desired line number inside the gutter area.
21
22
● 23 checkbox =
driver.find_element(...)
24 checkbox.click()
25 assert checkbox.is_selected()
The red circle indicates an active breakpoint.
How to Run the Test in Debug Mode?
Method 1
Right-click anywhere inside the test file and select:
Debug
↓
test_debugging_in_pycharm()
Method 2
Right-click directly on the test method:
test_debugging_in_pycharm()
↓
Debug
Method 3
Use PyCharm’s toolbar.
Run
↓
Debug...
↓
Select the Desired Configuration
PyCharm automatically creates debug configurations for most PyTest projects.
Running from the Command Line
Execute the following command:
py -3 -m pytest -s ^
"63_examples/test_02_debugging_in_pycharm.py"
Run all debugging examples together:
py -3 -m pytest -s ^
"63_examples/"
Note: The
-soption displays output generated by print statements while running PyTest.
Useful Debugger Controls
| Action | Shortcut |
|---|---|
| Resume Program | F9 |
| Step Over | F8 |
| Step Into | F7 |
| Step Out | Shift + F8 |
| Stop | Ctrl + F2 |
| Evaluate Expression | Alt + F8 |
These commands allow developers to navigate through Selenium scripts efficiently during debugging.
Variable Inspection
Suppose execution pauses here:
checkbox = driver.find_element(
By.ID,
"isAgeSelected"
)
PyCharm automatically displays:
Variables
↓
driver
↓
Chrome WebDriver
↓
checkbox
↓
WebElement
↓
driver.current_url
↓
Current Webpage URL
Variable inspection significantly simplifies debugging Selenium automation scripts.
Evaluate Expression
One of PyCharm’s most useful debugging tools is:
Evaluate Expression
↓
Alt + F8
For example, while execution is paused you can evaluate:
driver.title
driver.current_url
checkbox.is_selected()
driver.page_source
without modifying your Selenium script.
This capability is extremely useful while debugging automation failures.
Execution Flow
Launch Browser
│
▼
Open Website
│
▼
Breakpoint Reached
│
▼
Execution Paused
│
▼
Inspect Variables
│
▼
Evaluate Expressions
│
▼
Continue Execution
│
▼
Assertions Passed
│
▼
Close Browser
Automation Testing Example
Suppose an assertion fails unexpectedly.
Instead of writing:
assert checkbox.is_selected()
we can pause execution and evaluate:
checkbox.is_selected()
which may display:
False
allowing us to immediately identify the reason for the failed assertion.
Real-World Example
Large automation frameworks commonly use PyCharm debugging to investigate:
Failed assertions.
Incorrect locators.
Synchronization issues.
Missing elements.
Browser failures.
Framework-related problems.
Unexpected application behavior.
For example:
Automation Failure
│
▼
Breakpoint Reached
│
▼
Inspect Variables
│
▼
Evaluate Expressions
│
▼
Identify Failure
│
▼
Fix the Problem
│
▼
Run the Test Again
PyCharm significantly simplifies debugging in such situations.
Common Mistakes Beginners Make
Not Using Breakpoints
Many beginners rely entirely on:
print()
statements for debugging.
Although useful, breakpoints provide significantly more information during execution.
Ignoring Evaluate Expression
PyCharm’s Evaluate Expression tool allows developers to inspect:
Selenium WebElements.
Browser information.
Variable values.
Framework behavior.
Learning this feature greatly improves debugging efficiency.
Placing Breakpoints Everywhere
Avoid placing unnecessary breakpoints throughout your automation scripts.
Prefer placing them around:
Assertions.
Element interactions.
Synchronization points.
Complex business logic.
Best Practices
Use breakpoints strategically.
Utilize Evaluate Expression whenever appropriate.
Inspect variables before modifying the script.
Prefer debugger tools over excessive print statements.
Maintain meaningful assertions.
Use PyCharm’s integrated debugger while developing automation frameworks.
Debug synchronization issues carefully.
Conclusion
PyCharm provides powerful debugging capabilities that significantly simplify Selenium automation development. Breakpoints, variable inspection, Evaluate Expression, and PyTest integration make it easier to identify failures and understand application behavior during test execution.
Well-designed debugging practices improve framework reliability, simplify maintenance, and substantially reduce troubleshooting efforts in real-world automation projects.
Mastering PyCharm debugging techniques is an essential Selenium automation and interview skill.
Frequently Asked Questions (FAQs)
What is debugging in PyCharm?
It is the process of pausing program execution and inspecting its behavior using PyCharm’s debugging tools.
Why should I use breakpoints?
Breakpoints allow developers to:
Pause execution.
Inspect variables.
Evaluate expressions.
Analyze failures efficiently.
What is Evaluate Expression?
It is PyCharm’s debugging tool that allows developers to execute Python expressions during runtime without modifying the source code.
Can I debug PyTest tests directly in PyCharm?
Yes.
PyCharm provides built-in support for debugging PyTest-based Selenium tests.
Should I still use print statements?
Yes.
Print statements are useful for quick debugging, but PyCharm’s debugging tools provide substantially more information during execution.
Key Takeaways
PyCharm provides powerful debugging capabilities for Selenium automation testing.
Breakpoints allow developers to pause execution and inspect Selenium’s behavior efficiently.
Evaluate Expression is extremely useful for inspecting variables and WebElements during runtime.
Variable inspection significantly simplifies failure analysis.
PyTest tests can be debugged directly inside PyCharm.
Proper debugging practices substantially improve framework maintainability.
Strategic use of breakpoints greatly improves troubleshooting efficiency.
Debugging in PyCharm is an important Selenium automation and interview topic.
