Selenium: The Quick and Dirty Guide in 6 Steps
Selenium is a powerful tool for automating browser actions, but getting started can be daunting with all the comprehensive tutorials out there. This guide is designed to introduce you to the basics of Selenium quickly and simply, using Python. Let’s get started!
What You’ll Need
Before diving in, you’ll need a few essentials:
- Programming Language: Python
- IDE or Text Editor: Choose your favorite
- Web Driver: Chrome driver (installed via
brew cask install chromedriver
on Mac or downloaded for Windows) - Unit Test Framework: Pytest
Step 1: Setting Up
First, create a Python file for your test cases. We’ll name it test_cases.py
and define a class QuickAndDirtySeleniumExample
.
Import the necessary modules:
import unittest
from selenium import webdriver
class QuickAndDirtySeleniumExample(unittest.TestCase):
def test_search_proper_tutorial(self):
# This is our test case
if name == 'main':
unittest.main()
Step 2: Launch the Browser
Initialize a new Chrome WebDriver instance:
import unittest
from selenium import webdriver
class QuickAndDirtySeleniumExample(unittest.TestCase):
def test_search_proper_tutorial(self):
driver = webdriver.Chrome()
if name == 'main':
unittest.main()
Step 3: Navigate to Google
Use the get()
method to navigate to Google:
import unittest
from selenium import webdriver
class QuickAndDirtySeleniumExample(unittest.TestCase):
def test_search_proper_tutorial(self):
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
if name == 'main':
unittest.main()
Step 4: Locate the Search Box
Locate the Google search text box using find_element_by_name
:
import unittest
from selenium import webdriver
class QuickAndDirtySeleniumExample(unittest.TestCase):
def test_search_proper_tutorial(self):
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
searchbox = driver.find_element_by_name("q")
if name == 'main':
unittest.main()
Step 5: Perform a Search
Send a search query to the text box and press Enter:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class QuickAndDirtySeleniumExample(unittest.TestCase):
def test_search_proper_tutorial(self):
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
searchbox = driver.find_element_by_name("q")
searchbox.send_keys("selenium automation framework qaboy")
searchbox.send_keys(Keys.ENTER)
if name == 'main':
unittest.main()
Step 6: Click the Desired Link and Validate
Locate the link to the tutorial using XPath and click it. Then, validate the result using an assertion:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class QuickAndDirtySeleniumExample(unittest.TestCase):
def test_search_proper_tutorial(self):
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
searchbox = driver.find_element_by_name("q")
searchbox.send_keys("selenium automation framework qaboy")
searchbox.send_keys(Keys.ENTER)
link = driver.find_element_by_xpath(
"//a[@href='https://qaboy.com/2018/01/15/automated-framework-using-selenium-with-python/']")
link.click()
assert driver.find_element_by_class_name("entry-title"), "Element was not found."
if name == 'main':
unittest.main()
Run the Test
Execute the test using the following command:
python -m pytest test_cases.py
Ready to Dive Deeper?
While this quick guide introduces you to the basics of Selenium, creating a maintainable framework requires more structure and best practices. If you’re ready to take your Selenium skills to the next level, explore our comprehensive tutorial on building a robust Selenium framework.
Transform your testing today! Contact TechAID to learn how our QA services can help you implement efficient and effective testing strategies.