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:


pip install selenium

This installs the Selenium library.


Step 4: Download ChromeDriver

ChromeDriver is needed to control Chrome using Selenium.

  1. Go to: https://chromedriver.chromium.org/downloads

  2. Download the version that matches your Chrome browser

  3. Extract the file

  4. Note the path where chromedriver.exe is saved (Windows) or chromedriver (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:


from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time # Set up the driver (replace path with your own ChromeDriver path) driver_path = "C:/path/to/chromedriver.exe" # Windows # driver_path = "/usr/local/bin/chromedriver" # Mac/Linux # Create browser object driver = webdriver.Chrome(executable_path=driver_path) # Maximize window driver.maximize_window() # Step 1: Open Google driver.get("https://www.google.com") # Wait for page to load time.sleep(2) # Step 2: Find the search box search_box = driver.find_element(By.NAME, "q") # Step 3: Type the search keyword search_box.send_keys("Selenium tutorial") # Step 4: Press Enter search_box.send_keys(Keys.RETURN) # Step 5: Wait and get the title time.sleep(3) print("Page Title:", driver.title) # Step 6: Close browser driver.quit()

Output:

Page Title: Selenium tutorial - Google Search

Congratulations! πŸŽ‰ You just ran your first Selenium test!


Step 6: Explanation

LinePurpose
from selenium...Import Selenium classes
webdriver.ChromeLaunch 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 chromedriver path is correct and executable.

2. DeprecationWarning

  • Solution: Always use By.NAME, By.ID, etc. (not old find_element_by_* methods)

3. Browser closes too fast

  • Solution: Add time.sleep() or use input("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:


from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait up to 10 seconds for search box search_box = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.NAME, "q")) )

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.



Read More 




Comments

Popular posts from this blog

Tosca System Requirements and Installation Guide (Step-by-Step)

How to Install Selenium for Python Step-by-Step

Tosca Commander: A Beginner’s Overview