Setting up Python environment helps you write and run test scripts easily. It prepares your computer to use Python and Selenium for testing websites.
0
0
Python environment setup in Selenium Python
Introduction
When you want to start writing automated tests for a website.
When you need to install tools to control a browser with Python.
When you want to practice Selenium testing on your computer.
When you need to manage Python packages for testing.
When you want to run your test scripts without errors.
Syntax
Selenium Python
python -m venv env source env/bin/activate # On Mac/Linux env\Scripts\activate.bat # On Windows pip install selenium
python -m venv env creates a new folder named env for your Python environment.
Use source env/bin/activate on Mac/Linux or env\Scripts\activate.bat on Windows to start using this environment.
Examples
This creates and activates a Python environment called
mytestenv and installs Selenium inside it.Selenium Python
python -m venv mytestenv source mytestenv/bin/activate pip install selenium
On Windows, activate the environment and install a specific Selenium version.
Selenium Python
env\Scripts\activate.bat
pip install selenium==4.10.0Sample Program
This simple script opens Chrome, goes to example.com, prints the page title, then closes the browser.
Selenium Python
from selenium import webdriver # Start Chrome browser browser = webdriver.Chrome() # Open a website browser.get('https://example.com') # Print page title print(browser.title) # Close browser browser.quit()
OutputSuccess
Important Notes
Make sure you have ChromeDriver installed and it matches your Chrome browser version.
Activate your Python environment every time before running tests to use the right packages.
Use pip freeze to see installed packages and their versions.
Summary
Python environment setup creates a safe space for your test tools.
Activate the environment before installing or running Selenium tests.
Install Selenium to control browsers for automated testing.