Setting up Python environment helps you write and run test scripts easily. It prepares your computer to use Python and Selenium for testing websites.
Python environment setup in 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.
mytestenv and installs Selenium inside it.python -m venv mytestenv source mytestenv/bin/activate pip install selenium
env\Scripts\activate.bat
pip install selenium==4.10.0This simple script opens Chrome, goes to example.com, prints the page title, then closes the browser.
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()
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.
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.
Practice
Solution
Step 1: Understand virtual environment purpose
A virtual environment keeps project packages separate from global Python packages.Step 2: Relate to Selenium testing
This isolation prevents conflicts between different projects' dependencies, ensuring stable tests.Final Answer:
To isolate project dependencies and avoid conflicts -> Option BQuick Check:
Virtual environment = isolate dependencies [OK]
- Thinking virtual env speeds up browser
- Assuming it writes test scripts automatically
- Believing it changes screen resolution
venv on Windows?Solution
Step 1: Identify Windows activation command
On Windows, virtual environments activate withvenv\Scripts\activate.Step 2: Compare other options
source venv/bin/activateis for Unix/Linux; others are incorrect syntax.Final Answer:
venv\Scripts\activate -> Option CQuick Check:
Windows activate = venv\Scripts\activate [OK]
- Using Unix command on Windows
- Typing activate without path
- Confusing venv creation with activation
import selenium print(selenium.__version__)
What will happen if Selenium is not installed in this environment?
Solution
Step 1: Understand import behavior
If a module is not installed, Python raises ModuleNotFoundError on import.Step 2: Check code effect
Since Selenium is missing,import seleniumfails with error, no print occurs.Final Answer:
It raises a ModuleNotFoundError -> Option AQuick Check:
Missing module import = ModuleNotFoundError [OK]
- Expecting automatic install
- Thinking it prints None
- Assuming version prints without error
venv\Scripts\activate but got an error. What is the likely problem?Solution
Step 1: Identify path style mismatch
Linux uses forward slashes andvenv/bin/activate, not Windows backslashes.Step 2: Understand error cause
Using Windows path on Linux causes 'file not found' error when activating.Final Answer:
Using Windows path on Linux -> Option DQuick Check:
Linux activate path uses forward slashes [OK]
- Assuming virtual env not created
- Blaming Python installation
- Confusing Selenium install with activation
Solution
Step 1: Create virtual environment first
Usepython -m venv envto create isolated environment.Step 2: Activate environment before installing packages
Activate withsource env/bin/activateso pip installs inside it.Step 3: Install Selenium inside activated environment
Runpip install seleniumto add Selenium only to this project.Final Answer:
python -m venv env; source env/bin/activate; pip install selenium -> Option AQuick Check:
Create, activate, then install Selenium [OK]
- Installing Selenium before activating env
- Activating before creating env
- Skipping environment creation
Ready to prove it?
Pass the test and this pattern moves unknown
