Setting Up Your First Selenium Python Script
Selenium is a powerful and widely-used tool for automating web browsers. Whether you're testing a web application or scraping data from websites, Selenium with Python is a great starting point for both beginners and experienced developers. In this blog, we'll walk through how to set up your first Selenium Python script from scratch, including installation, writing basic commands, and running your first test.
What is Selenium?
Selenium is an open-source framework for automating web browsers. It supports various languages like Python, Java, C#, and JavaScript, and it works across major browsers like Chrome, Firefox, and Edge. With Selenium WebDriver, you can simulate real user actions such as clicking buttons, entering text, and navigating pages.
Prerequisites
Before we start writing code, make sure you have the following installed:
- Python (preferably version 3.7 or higher)
- pip (Python package installer)
- Google Chrome (or any browser you prefer)
You can check if Python and pip are installed by running:
bash
python --version
pip --version
Step 1: Install Selenium
To use Selenium with Python, you need to install the Selenium library via pip:
bash
pip install selenium
This command will download and install the latest version of Selenium for Python.
Step 2: Download the Browser Driver
Each browser requires a specific driver to work with Selenium. For Chrome, you’ll need ChromeDriver:
- Visit: https://sites.google.com/chromium.org/driver/
- Download the version that matches your installed Chrome browser.
- Unzip the file and place the executable in a known path (e.g., C:\WebDrivers or /usr/local/bin).
Make sure the driver is accessible in your system's PATH, or you’ll need to provide the full path in your script.
Step 3: Write Your First Script
Create a Python file named test_selenium.py and add the following code:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Set up the Chrome WebDriver
driver = webdriver.Chrome() # or provide path: webdriver.Chrome(executable_path="path/to/chromedriver")
# Navigate to a website
driver.get("https://www.google.com")
# Wait for a moment
time.sleep(2)
# Find the search box, type a query, and submit
search_box = driver.find_element(By.NAME, "q")
search_box.send_keys("Selenium Python")
search_box.submit()
# Wait to see results
time.sleep(3)
# Print the page title
print("Page title is:", driver.title)
# Close the browser
driver.quit()
Step 4: Run the Script
Open your terminal or command prompt, navigate to the script location, and run:
bash
python test_selenium.py
A browser window will open, navigate to Google, enter a search query, and close automatically after a few seconds.
Troubleshooting Tips
- Driver not found? Ensure the driver is installed and in your system’s PATH.
- Browser mismatch? Match the driver version with your browser version.
- Element not found? Wait for the page to load or use WebDriverWait for dynamic elements.
Next Steps
Now that you’ve run your first script, explore more advanced Selenium features:
- Interact with buttons, dropdowns, and forms
- Handle alerts, frames, and windows
- Implement waits (implicitly_wait or WebDriverWait)
- Build structured test cases using unittest or pytest
Conclusion
Setting up your first Selenium Python script is simple and rewarding. With just a few lines of code, you can automate real browser actions, making testing faster and more reliable. As you grow more comfortable, Selenium opens the door to building robust automation frameworks for web applications, whether for testing, monitoring, or data extraction.
Learn Selenium with Python Training Course
Read More:
Visit Quality Thought Training Institute in Hyderabad
Get Direction
Comments
Post a Comment