Automated testing plays a crucial role in modern software development, ensuring that applications function correctly across different environments. Selenium is one of the most popular frameworks for web automation testing, allowing testers and developers to automate interactions with web applications.
To efficiently write and execute Selenium scripts, a robust Integrated Development Environment (IDE) is essential. PyCharm, developed by JetBrains, is a powerful and widely used IDE for Python development. It offers intelligent code completion, debugging tools, and seamless integration with Selenium, making it an excellent choice for test automation.
This guide, explores the step-by-step process of setting up Selenium in PyCharm, from installation to running your first test script.
What is PyCharm?
PyCharm is a professional Python IDE developed by JetBrains, designed to provide an efficient and user-friendly coding experience. It comes with a range of features that help developers and testers write clean, maintainable, and high-performing Python code.
With its robust capabilities, PyCharm simplifies Selenium test automation, making it easier to write, execute, and manage test scripts efficiently.
Key Features of PyCharm:
- Intelligent Code Assistance– Auto-completion, quick fixes, and syntax highlighting.
- Built-in Debugging & Testing– Integrated debugging tools and unit testing support.
- Version Control Integration– Supports Git, SVN, and Mercurial for seamless collaboration.
- Virtual Environment & Package Management– Easily install and manage dependencies like Selenium.
- Cross-Platform Support- Available on Windows, macOS, and Linux.
Why use Selenium with PyCharm?
Selenium is a powerful tool for automating web applications, and PyCharm provides an efficient, feature-rich environment to write and manage Selenium test scripts.
By combining Selenium’s automation capabilities with PyCharm’s powerful development tools, testers and developers can enhance productivity, streamline testing workflows, and ensure reliable web application testing.
Using Selenium with PyCharm offers several advantages:
- PyCharm’s intelligent code completion, syntax highlighting, and debugging tools help in writing error-free Selenium test scripts.
- Built-in virtual environment and package management make it easy to install and manage Selenium and other dependencies.
- Supports pytest, unittest, and other testing frameworks, simplifying test case structuring and execution.
- Integrated Git, GitHub, and other version control systems enable seamless collaboration on test automation projects.
- PyCharm runs on Windows, macOS, and Linux, offering flexibility for different testing environments.
Setting up Selenium with PyCharm
Before setting up Selenium in PyCharm, ensure you have the following prerequisites:
- Python Installed – Selenium requires Python to run. Download and install the latest version from python’s official site.
- PyCharm Installed – Download and install PyCharm (Community or Professional edition) from jetbrains.
- pip (Python Package Manager) – Comes pre-installed with Python and is used to install Selenium and other dependencies.
Once these prerequisites are met, you’re ready to set up Selenium in PyCharm. In the next section, we’ll guide you through the installation and configuration process.
Configuring PyCharm for Selenium
Now that the prerequisites are in place, follow these steps to install and configure Selenium in PyCharm:
Step 1: Create a New Python Project in PyCharm
1. Open PyCharm and click on “New Project”.
2. Choose a project location and ensure that Virtualenv is selected as the environment.
3. Click “Create” to set up your project.
Step 2: Install Selenium via pip
1. Open the Terminal in PyCharm. (Marked in below image)
2. Run the following command to install Selenium:
pip install selenium
3. Wait for the installation to complete. You should see a success message confirming Selenium is installed.
Step 3: Verify Installation
1. Open the Python Console in PyCharm.
2. Run the following command to check if Selenium is installed correctly:
import selenium print(selenium.__version__)
3. If no errors appear, Selenium is successfully installed.
With Selenium installed and configured in PyCharm, you’re now ready to write and execute your first Selenium test script.
Testing Website on Chrome and Firefox using Selenium PyCharm: Example
Now that Selenium is installed, let’s write a simple test script to open a demo website in both Chrome and Firefox using Selenium in PyCharm and print it’s title.
Step 1: Write the Selenium Script
Create a new Python file (e.g., test_script.py) and add the following code:
from selenium import webdriver # Launch Chrome chrome_driver = webdriver.Chrome() chrome_driver.get('https://bstackdemo.com/') print("Title in Chrome:", chrome_driver.title) chrome_driver.quit() # Launch Firefox firefox_driver = webdriver.Firefox() firefox_driver.get("https://bstackdemo.com/") print("Title in Firefox:", firefox_driver.title) firefox_driver.quit()
Step 2: Run the Script
1. In PyCharm, open the Terminal and run below command.
python file_name.py
2. The script will:
- Open Chrome and Firefox to visit “https://bstackdemo.com“.
- Print the website title in both browsers like below screenshot.
- Close the browsers after execution.
This example demonstrates how to test websites across multiple browsers using Selenium in PyCharm efficiently.
Debugging Tests in Selenium with PyCharm
Debugging helps identify and fix issues in Selenium test scripts efficiently. PyCharm provides powerful debugging tools such as breakpoints, variable inspection, and step-by-step execution control.
By setting breakpoints in the script, testers can pause execution and analyze the state of variables, element locators, and browser interactions. Running the script in Debug Mode (Shift + F9) allows stepping through code, inspecting variable values, and catching errors in real time. The Console output helps track stack traces and exceptions for quick issue resolution.
We can use print() for debugging, which provides basic output but lacks control over log levels and file storage. However, the logging module allows better debugging with different log levels (INFO, WARNING, ERROR) and the ability to store logs for later analysis, so usually, we use logging.
Example
import logging from selenium import webdriver # Configure logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") driver = webdriver.Chrome() driver.get("https://bstackdemo.com/") # Set a breakpoint or inspect logs title = driver.title logging.info(f"Page Title: {title}") driver.quit()
With logging, test execution details are recorded systematically, making debugging more effective and manageable.
Note: If using the pytest framework, we can configure the logging in the pytest.ini file as below:
[pytest] log_cli = true log_cli_level = INFO log_cli_format = %(asctime)s - %(levelname)s - %(message)s
Best Practices for Using PyCharm with Selenium
Following best practices while using PyCharm with Selenium helps improve code efficiency, maintainability, and debugging capabilities.
- Use Virtual Environments: Create a virtual environment for your Selenium project to avoid dependency conflicts.
- Follow Proper Logging: Instead of using print(), utilize Python’s logging module for better debugging and log management.
- Use Explicit Waits: Rely on explicit waits (WebDriverWait) instead of implicit waits or time.sleep() for better test stability.
- Organize Test Scripts: Structure your test scripts using functions or frameworks like pytest for better reusability and scalability.
- Enable PyCharm’s Code Assistance: Use PyCharm’s built-in inspections, code suggestions, and auto-formatting for cleaner code.
- Use Page Object Model (POM): Implement POM to separate test logic from UI interactions, making test maintenance easier.
- Debug Efficiently: Utilize PyCharm’s debugger (Shift + F9) and breakpoints for efficient troubleshooting.
Why Use BrowserStack Automate to Run Selenium Tests on Real Devices?
Running Selenium tests on real devices ensures accurate results by replicating real-world user conditions, including different browsers, OS versions, and device hardware. However, maintaining an in-house device lab is costly and time-consuming.
BrowserStack Automate provides a cloud-based real-device testing platform, allowing Selenium tests to run on thousands of real devices and browsers without setup or maintenance hassles. It offers:
- Access to Real Devices & Browsers: Test across a vast range of devices, browsers, and OS combinations.
- Parallel Testing: Run multiple tests simultaneously, reducing execution time.
- Zero Setup & Maintenance: No need to install or manage physical devices or browser drivers.
- Accurate Debugging Tools: Provides screenshots, video recordings, and logs for faster issue resolution.
- Seamless Integration: Works with CI/CD tools like Jenkins, GitHub Actions, and Azure DevOps for continuous testing.
By using BrowserStack Automate, teams can achieve faster, more reliable, and scalable Selenium test execution on real devices.
Conclusion
Setting up Selenium with PyCharm simplifies browser automation, providing robust debugging and code management features. Proper installation, configuration, and best practices like using logging, explicit waits, and structured test scripts improve efficiency and test reliability.
Integrating cloud platforms like BrowserStack Automate ensures cross-browser compatibility on real devices. By leveraging Selenium with PyCharm effectively, teams can build scalable and reliable test automation frameworks for faster and more accurate testing.