Setting Up Selenium in PyCharm or VS Code
Setting Up Selenium in PyCharm or VS Code
Selenium is a powerful tool for automating web browsers, and you can easily set it up using PyCharm or VS Code. This guide walks you through installing and configuring Selenium in both IDEs.
✅ Prerequisites
Python installed (Python 3.6+ recommended)
Pip (comes with Python)
Chrome browser (or another supported browser)
ChromeDriver (or relevant WebDriver)
1. Installing Selenium
Open your terminal or command prompt and run:
bash
Copy
Edit
pip install selenium
To check the version:
bash
Copy
Edit
pip show selenium
2. Download the WebDriver
For Chrome:
Go to https://chromedriver.chromium.org/downloads
Download the version that matches your Chrome browser
Unzip and place chromedriver (or chromedriver.exe on Windows) in a known folder (e.g., project root or /usr/local/bin)
3. Setting Up in PyCharm
Step-by-Step:
Create a New Project:
Open PyCharm → New Project
Select Python interpreter (you can create a new virtual environment)
Install Selenium in PyCharm:
Go to File → Settings → Project → Python Interpreter
Click the + button → Search for selenium → Install
Write a Sample Script:
python
Copy
Edit
from selenium import webdriver
driver = webdriver.Chrome(executable_path='path/to/chromedriver')
driver.get("https://www.google.com")
print(driver.title)
driver.quit()
Run the Script:
Right-click the file → Run
4. Setting Up in VS Code
Step-by-Step:
Create a New Python File:
Open VS Code → New File → Save as test_selenium.py
Set Up Python Environment:
Install the Python extension for VS Code (if not installed)
Open a terminal in VS Code:
bash
Copy
Edit
pip install selenium
Write a Sample Script (same as above)
Run the Script:
In terminal: python test_selenium.py
Or click the Run button in VS Code
5. Sample Selenium Script
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path='path/to/chromedriver') # Use full path or put chromedriver in PATH
driver.get("https://www.google.com")
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium Python")
search_box.submit()
time.sleep(3) # Let page load
print(driver.title)
driver.quit()
Note: Starting with Selenium 4, executable_path is deprecated if ChromeDriver is in PATH. Use:
python
Copy
Edit
driver = webdriver.Chrome()
✅ Tips & Best Practices
Always match the WebDriver version to your browser version.
Use webdriver-manager to avoid manual driver downloads:
bash
Copy
Edit
pip install webdriver-manager
Then use:
python
Copy
Edit
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
Handle delays using WebDriverWait instead of time.sleep().
Close your driver with driver.quit() to free resources.
✅ Conclusion
Setting up Selenium in PyCharm or VS Code is straightforward. With just a few steps, you can begin writing powerful browser automation scripts. Choose the IDE that suits your workflow best, and leverage Selenium’s features for testing, scraping, or automating web tasks.
Learn Selenium Python Training in Hyderabad
Read More
Top Tools You Need to Start Selenium with Python
How to Automate Web Testing Using Selenium and Python
Python vs Other Languages for Selenium: Pros & Cons
Understanding the Role of WebDriver in Automation
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment