Your First Selenium Test Script in Python
Your First Selenium Test Script in Python
Introduction
Selenium is one of the most popular tools for automating web applications.
With Selenium and Python, you can control a browser just like a human user.
In this article, you'll:
✅ Set up your environment
✅ Install Selenium
✅ Write your first script
✅ Run a real browser test
Let’s get started! π
Step 1: What Is Selenium?
Selenium is an open-source automation tool used to:
-
Test web applications
-
Automate browser actions like clicking, typing, scrolling, etc.
-
Run tests on Chrome, Firefox, Safari, and more
We’ll use the Selenium WebDriver with Python to automate a browser.
Step 2: Prerequisites
Make sure you have:
✅ Python installed (version 3.6 or above)
✅ A code editor (VS Code or any IDE)
✅ Internet access
✅ Google Chrome browser installed
Step 3: Install Selenium
Open your terminal or command prompt and type:
This installs the Selenium library.
Step 4: Download ChromeDriver
ChromeDriver is needed to control Chrome using Selenium.
-
Download the version that matches your Chrome browser
-
Extract the file
-
Note the path where
chromedriver.exeis saved (Windows) orchromedriver(Mac/Linux)
Step 5: Your First Script – Open Google and Search
Now let’s write a simple test to:
-
Open Chrome
-
Go to Google
-
Type "Selenium tutorial"
-
Click Search
-
Print the title of the results page
Full Script:
Output:
Congratulations! π You just ran your first Selenium test!
Step 6: Explanation
| Line | Purpose |
|---|---|
from selenium... | Import Selenium classes |
webdriver.Chrome | Launch Chrome browser |
driver.get() | Open a URL |
find_element() | Locate input box |
send_keys() | Type and press Enter |
time.sleep() | Pause so page loads |
driver.quit() | Close the browser |
Step 7: Common Errors and Fixes
1. WebDriverException: driver not found
-
Solution: Check if
chromedriverpath is correct and executable.
2. DeprecationWarning
-
Solution: Always use
By.NAME,By.ID, etc. (not oldfind_element_by_*methods)
3. Browser closes too fast
-
Solution: Add
time.sleep()or useinput("Press enter to exit...")at the end.
Step 8: Best Practices
✅ Always use explicit waits (WebDriverWait) instead of time.sleep()
✅ Keep driver path in a config file or environment variable
✅ Use Page Object Model (POM) for large projects
✅ Use virtual environments for Python projects
✅ Handle exceptions properly
Step 9: Use Explicit Wait (Better Way)
Here’s a cleaner way using WebDriverWait:
This method is more reliable than using time.sleep().
Step 10: Try More Selenium Actions
Explore more things you can do:
-
Click buttons:
element.click() -
Select from dropdown:
Select(element).select_by_visible_text("Option") -
Get element text:
element.text -
Take screenshot:
driver.save_screenshot("screenshot.png") -
Scroll page:
driver.execute_script("window.scrollTo(0, 500)")
Conclusion
Selenium with Python is a powerful combo for browser automation.
In this article, you learned how to:
✅ Set up Selenium
✅ Install ChromeDriver
✅ Write and run your first test
✅ Search on Google automatically
✅ Use waits and best practices
You’re now ready to explore more advanced test cases and frameworks like PyTest, Behave, or Robot Framework.
Comments
Post a Comment