Selenium Python Tutorial (with Example)

Test Selenium Python on Real Devices and Browsers for accurate test results under real user conditions

AI in Testing: Learn How!
Home Guide Selenium Python Tutorial (with Example)

Selenium Python Tutorial (with Example)

Most developers think running Selenium with Python is just about opening a browser, finding elements, and clicking buttons. That seems straightforward and simple, and many tutorials make it look effortless.

But in reality, following this basic path often leads to slow tests, unexpected failures, and hours spent debugging simple issues. Selenium is far more powerful than just automating clicks, yet most people never tap into its full potential.

What if I told you that with a few strategic setups and practices, you can write scripts that run reliably across any browser, catch errors automatically, and save hours of debugging?

Struggling with Selenium Python?

Talk to QA experts who have been in your shoes and get advice on setup, best practices etc.

In this tutorial, I’ll show how I run Selenium with Python by writing robust scripts that control browsers like a pro.

Why do Developers Prefer Python for Writing Selenium Test Scripts in 2026?

Developers prefer Python for writing Selenium test scripts because of its simplicity, readability, and ease of use. Python’s clear and concise syntax allows for faster script development and easier maintenance, which is crucial in testing scenarios.

Additionally, Python has a rich set of libraries and frameworks that complement Selenium, making it easier to handle complex tasks such as data manipulation, reporting, and integration with other tools.

Python’s extensive community support and documentation also provide valuable resources for troubleshooting and improving test scripts. These factors make Python a popular choice for Selenium automation.

Getting Started with Selenium Python in 2026

Getting started with Selenium using Python involves setting up an environment where you can write and run automated test scripts for web applications.

Selenium, combined with Python, offers a powerful and easy-to-learn toolset for automating browser interactions. Python’s simple syntax makes it ideal for quickly writing clear and maintainable test scripts.

To begin, you’ll need to install the Selenium WebDriver, set up a compatible browser, and learn the basics of locating web elements, interacting with them, and running test cases. This combination is perfect for testing dynamic and responsive web applications efficiently.

What is Selenium WebDriver

Selenium WebDriver is the core component that drives the browser for automation. It acts like a remote control, allowing scripts to open pages, click buttons, fill forms, and check results, just like a real user would. Each major browser (such as Chrome, Firefox, and Edge) has its own version of the WebDriver to ensure compatibility and control.

What Are Selenium Python Bindings?

Selenium Python bindings are a set of libraries that let Python interact with Selenium WebDriver. These libraries provide the tools and commands needed to control a web browser using Python code.

In simple terms, the bindings act as a bridge between Selenium’s core functions and Python. They allow test scripts written in Python to open web pages, click buttons, enter text, and check results, just like how a real user would do in the browser.

The Python bindings are officially supported by the Selenium team and are easy to install using pip. Once installed, they make it possible to write powerful browser automation scripts using clean and readable Python syntax.

Selenium Python Example: How to run your first Test?

To run Selenium Python Tests here are the steps to follow:

Step 1. Import the Necessary Classes

First, you’ll need to import the WebDriver and Keys classes from Selenium. These classes help you interact with a web browser and emulate keyboard actions.

from selenium import webdriver

from selenium.webdriver.common.keys import Keys
  • webdriver: Allows you to control the browser.
  • Keys: Lets you simulate keyboard key presses.

Step 2. Create a WebDriver Instance

To interact with a browser, you’ll need to create an instance of WebDriver. In this example, we use Chrome:

driver = webdriver.Chrome('./chromedriver')

Make sure chromedriver is in the same directory as your Python script. This command opens a new Chrome browser window.

Step 3. Load a Website

Use the .get() method to navigate to a website. This method waits for the page to load completely:

driver.get("https://www.python.org")

This will open Python’s official website in the browser.

Step 4. Check the Page Title

Once the page is loaded, you can retrieve and print the page title to verify you’re on the right page:

print(driver.title)

You should see:

Welcome to Python.org

Step 5. Interact with the Search Bar

To perform a search, locate the search bar element, enter a query, and submit it. Here’s how to find the search bar by its name attribute and interact with it:

search_bar = driver.find_element_by_name("q")

search_bar.clear()

search_bar.send_keys("getting started with python")

search_bar.send_keys(Keys.RETURN)

As an explanation :

  • find_element_by_name(“q”): Finds the search bar element.
  • clear(): Clears any existing text.
  • send_keys(“getting started with python”): Types the query into the search bar.
  • send_keys(Keys.RETURN): Simulates pressing the Return (Enter) key.

Step 6. Verify the Resulting URL

After submitting the search query, you can check the updated URL to confirm the search results page:

print(driver.current_url)

You should see a URL similar to:

https://www.python.org/search/?q=getting+started+with+python&submit=

Step 7. Close the Browser

Finally, close the browser session to end the test:

driver.close()

Summary :

Here is the complete script for your first Selenium test in Python. Save this code in a file named selenium_test.py and run it using python selenium_test.py:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys



# Create a new instance of the Chrome driver

driver = webdriver.Chrome('./chromedriver')



# Open the Python website

driver.get("https://www.python.org")



# Print the page title

print(driver.title)



# Find the search bar using its name attribute

search_bar = driver.find_element_by_name("q")

search_bar.clear()

search_bar.send_keys("getting started with python")

search_bar.send_keys(Keys.RETURN)



# Print the current URL

print(driver.current_url)



# Close the browser window

driver.close()

Selenium Components Overview

Selenium provides a suite of tools designed for different testing scenarios, from quick script recording to large-scale parallel execution.

1. Selenium IDE

Selenium IDE is a browser extension available for Chrome and Firefox that allows you to record and replay user interactions. It’s the fastest way to create automation scripts without writing code manually.

Key features of Selenium IDE:

  • Record and Playback: Capture clicks, form entries, navigation, and other interactions to generate test scripts automatically.
  • Export to Code: Recorded tests can be exported into multiple programming languages, including Python, Java, and JavaScript, making it easy to transition from a beginner-friendly IDE to full-fledged Selenium scripts.
  • Command Library: Provides commands for assertions, verifications, waits, and element interactions, enabling more complex automation without touching the underlying code.
  • Plugins and Extensions: Supports extensions for additional reporting, integration with CI/CD pipelines, and custom command creation.

Use cases:

  • Rapid prototyping of tests for web applications
  • Generating scripts for repetitive tasks
  • Learning Selenium basics before moving to code-based frameworks

Limitations:

  • Not suitable for large-scale, cross-browser testing
  • Limited flexibility for handling dynamic web applications

2. Selenium Grid

Selenium Grid is designed for parallel testing across multiple machines, browsers, and operating systems. It allows teams to scale their test automation and ensure consistent behavior across environments.

Key features:

  • Distributed Testing: Run tests simultaneously on different machines, reducing overall test execution time.
  • Cross-Browser Testing: Supports Chrome, Firefox, Edge, Safari, and more, enabling validation in the environments your users actually use.
  • Hub and Node Architecture: A central hub distributes test scripts to registered nodes, which can be configured with different OS and browser combinations.
  • Integration with CI/CD: Works seamlessly with tools like Jenkins, GitHub Actions, and GitLab for automated test execution on every build.

Use cases:

  • Large regression test suites
  • Ensuring application behavior is consistent across browsers and platforms
  • Speeding up automated test execution for faster feedback

Limitations:

  • Requires setup and maintenance of hub and nodes
  • Network or configuration issues can affect test reliability

3. Supported Browsers and Languages

Selenium is language-agnostic and supports nearly all major web browsers, providing flexibility to integrate into your existing development and testing workflows.

Key details:

  • Browsers: Chrome, Firefox, Edge, Safari, Internet Explorer, and headless browsers for faster execution.
  • Programming Languages: Python, Java, C#, Ruby, JavaScript, Kotlin, and more.
  • WebDriver API: Provides a unified interface to control browsers programmatically, allowing precise control over navigation, element interactions, waits, alerts, cookies, and more.
  • Community and Ecosystem: Selenium has a large, active community, extensive documentation, and third-party integrations for reporting, logging, and CI/CD pipelines.

Use cases:

  • Ensuring your tests are not limited by a single language or browser
  • Adapting Selenium to team skill sets and existing tech stacks
  • Leveraging WebDriver for advanced browser control and custom automation

Applications of Selenium in 2026

Selenium has grown beyond basic browser automation and remains a key tool for modern web testing and workflow automation. Its versatility makes it essential for teams aiming to deliver reliable, high-quality web applications efficiently.

  • Cross-Browser Functional Testing: Validate core functionalities across Chrome, Firefox, Edge, Safari, and more, ensuring consistent user experiences.
  • Regression Testing in CI/CD: Automatically run test suites on every build to catch broken features early and maintain release quality.
  • Automating Repetitive Web Tasks: Handle bulk data entry, scraping, and routine workflows to save time and reduce errors.
  • Visual Validation and Performance Checks: Detect UI regressions, layout shifts, and monitor page performance across devices.
  • Testing Dynamic Web Applications: Manage asynchronous content, client-side rendering, and interactive elements reliably.
  • Integration with AI and ML Workflows: Prioritize tests intelligently and generate scenarios based on user behavior and risk analysis.
  • Support for Hybrid and Micro-Frontend Architectures: Test individual components or end-to-end flows in modern distributed frontends.

Interacting with Common Elements in Selenium

Selenium allows you to perform a variety of actions on web elements. You have already touched upon entering input, here’s how to interact with buttons, and dropdowns:

Assuming you want to click a button with the ID “submit-button” after entering the input in the search bar :

# Locate the button by its ID attribute

button = driver.find_element_by_id("submit-button")



# Click the button

button.click()

If you need to click a link by its text:

# Locate the link by its link text

link = driver.find_element_by_link_text("Click Here")



# Click the link

link.click()

Explanation:

  • find_element_by_id(“submit-button”): Finds the button with the ID “submit-button”.
  • find_element_by_link_text(“Click Here”): Finds a link with the text “Click Here”.
  • click(): Simulates a mouse click on the element.

Though dropdowns are not present on this site, they are quite common for web application testing

For dropdown menus, Selenium provides the Select class to handle options within <select> elements.

Example: Selecting an Option from a Dropdown

Assuming you have a dropdown menu with the ID “dropdown-menu”:

from selenium.webdriver.support.ui import Select



# Locate the dropdown menu by its ID attribute

dropdown = Select(driver.find_element_by_id("dropdown-menu"))




# Select an option by visible text

dropdown.select_by_visible_text("Option 1")


# Or select an option by value

dropdown.select_by_value("option1")



# Or select an option by index (0-based index)

dropdown.select_by_index(0)

Explanation:

  • Select(driver.find_element_by_id(“dropdown-menu”)): Creates a Select object for the dropdown menu.
  • select_by_visible_text(“Option 1”): Selects an option by its visible text.
  • select_by_value(“option1”): Selects an option by its value attribute.
  • select_by_index(0): Selects an option by its index in the dropdown.

To optimize your Selenium interactions and troubleshoot common issues, expert guidance can make a significant difference.

Get Expert QA Guidance Today

Schedule a call with BrowserStack QA specialists to discuss your testing challenges, automation strategies, and tool integrations. Gain actionable insights tailored to your projects and ensure faster, more reliable software delivery.

Struggling with Selenium Python?

Talk to QA experts who have been in your shoes and get advice on setup, best practices etc.

Navigate through HTML DOM Elements

The HTML Document Object Model (DOM) represents the structure of a web page as a tree of objects. Selenium allows you to interact with these elements using various locator strategies.

In our first test script, we have already used some of the methods used to navigate DOM elements. This section will be a slightly more detailed view into how you can use different methods to locate and interact with elements on the Python.org website.

Step 1. Locate and Interact with Navigation Links

Let’s consider an example: Clicking the ‘Downloads’ link

To click the “Downloads” link, you can use the .find_element_by_link_text() method, but here’s how to use other locators to achieve the same, example by using find_element_by_xpath:

from selenium import webdriver



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open the Python website

driver.get("https://www.python.org/")



# Locate the "Downloads" link using XPath

downloads_link = driver.find_element_by_xpath("//a[text()='Downloads']")



# Click the "Downloads" link

downloads_link.click()



# Optionally, print the current URL to confirm navigation

print(driver.current_url)



# Close the browser

driver.close()

Explanation:

XPath: //a[text()='Downloads']

locates the “Downloads” link based on its visible text.

Step 2. Access and Interact with Header Sections

Let’s consider an example: Accessing the Main Header

To access the main header text, you can use different locators to find the header element.

Using find_element_by_class_name:

from selenium import webdriver



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open the Python website

driver.get("https://www.python.org/")



# Locate the header element using its class name

header = driver.find_element_by_class_name("introduction")



# Print the text of the header

print(header.text)



# Close the browser

driver.close()

Explanation:

  • Class Name: “introduction” is used to find the header element based on its class.

Step 3. Interact with Forms and Input Fields

Let’s consider an example: Filling Out and Submitting the Search Form

To interact with the search form, you can use the .find_element_by_name() method to locate the input field.

Using find_element_by_name:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open the Python website

driver.get("https://www.python.org/")



# Locate the search bar using its name attribute

search_bar = driver.find_element_by_name("q")



# Clear any existing text and enter a new search term

search_bar.clear()

search_bar.send_keys("Python Documentation")

search_bar.send_keys(Keys.RETURN)



# Optionally, print the current URL to confirm search results

print(driver.current_url)



# Close the browser

driver.close()

Explanation:

Name Attribute: find_element_by_name(“q”) locates the search input field by its name attribute.

Handling Waits in Selenium Python

Web applications often include dropdown menus (also known as select boxes) to let users choose an option from a list. Selenium provides a straightforward way to interact with these using the Select class.

Working with Dropdown Menus

To handle dropdowns, Selenium uses the Select class from the selenium.webdriver.support.ui module. This class allows selecting options by visible text, index or value.

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import Select




# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')




# Open a website with a dropdown (example URL)

driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select")




# Switch to the iframe containing the dropdown

driver.switch_to.frame("iframeResult")




# Locate the dropdown element

dropdown_element = driver.find_element(By.ID, "cars")




# Create a Select object

dropdown = Select(dropdown_element)




# Select an option by visible text

dropdown.select_by_visible_text("Volvo")




# Select an option by value

dropdown.select_by_value("saab")




# Select an option by index (starting at 0)

dropdown.select_by_index(2)




# Close the browser

driver.quit()

Explanation:

  • Select(element): Creates a Select object linked to the dropdown element.
  • select_by_visible_text(“text”): Chooses the option that matches the visible label.
  • select_by_value(“value”): Chooses based on the HTML value attribute.
  • select_by_index(n): Chooses the option at a specific position in the list.

Other Dropdown Actions:

  • dropdown.options: Returns all available options as a list.
  • dropdown.first_selected_option: Gets the currently selected option.

Dynamic content can load at different times, so using waits helps ensure elements are present before interacting with them.

Step 1. Implicit Waits

Example: Using Implicit Waits

from selenium import webdriver



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Set implicit wait

driver.implicitly_wait(10)  # seconds



# Open the Python website

driver.get("https://www.python.org/")



# Locate an element with implicit wait

search_bar = driver.find_element_by_name("q")

search_bar.send_keys("Python")



# Close the browser

driver.quit()

Explanation: implicitly_wait() sets a default wait time for finding elements. If an element is not immediately found, WebDriver will wait up to the specified time.

Step 2. Explicit Waits

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open the Python website

driver.get("https://www.python.org/")



# Define WebDriverWait with a maximum wait time of 10 seconds

wait = WebDriverWait(driver, 10)



# Wait for the search bar to be present in the DOM

search_bar = wait.until(EC.presence_of_element_located((By.NAME, "q")))



# Perform actions on the search bar

search_bar.send_keys("Python")



# Close the browser

driver.quit()

Explanation:

  • WebDriverWait(driver, 10): Creates an instance of WebDriverWait, specifying a maximum wait time of 10 seconds.
  • wait.until(EC.presence_of_element_located((By.NAME, “q”))): Pauses the script until the search bar element is found by its name attribute. If the element is not found within 10 seconds, a TimeoutException will be raised.

Assertions and Validations

To ensure that the application behaves as expected, you can use assertions and validations.

Verifying Expected Conditions Using Assertions

Example: Verifying Page Title and Search Results

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open the Python website

driver.get("https://www.python.org/")



# Use WebDriverWait to wait for the search bar to be present

wait = WebDriverWait(driver, 10)

search_bar = wait.until(EC.presence_of_element_located((By.NAME, "q")))



# Perform search

search_bar.send_keys("Python")

search_bar.send_keys(Keys.RETURN)



# Verify the title contains "Python"

assert "Python" in driver.title



# Verify search results contain expected text

results = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "ul.list-recent-events")))

assert "Python" in results.text



# Print the results to verify

print(driver.title)

print(results.text)



# Close the browser

driver.quit()

Explanation:

  • Assertions: Used to check if the conditions are met. For example, checking if the title or text of elements matches expected values.
  • assert: Verifies conditions and will raise an AssertionError if the condition is not true.

Handling Alerts and Pop-ups

Web applications often use JavaScript alerts, confirmation dialogs, or prompts to interact with users. Selenium provides ways to handle these pop-ups effectively.

Dealing with JavaScript Alerts

JavaScript alerts are simple pop-up messages that require user interaction to dismiss. Selenium allows you to interact with these alerts using the switch_to.alert() method.

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open a website that triggers an alert (example URL)

driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/Alert.html")



# Click a button that triggers an alert

trigger_alert_button = driver.find_element(By.ID, "alertButton")  # Adjust locator as needed

trigger_alert_button.click()



# Switch to the alert and accept it

alert = driver.switch_to.alert

print("Alert text:", alert.text)

alert.accept()



# Close the browser

driver.quit()

Explanation:

  • switch_to.alert: Switches the context to the alert. Once switched, you can interact with the alert.
  • alert.accept(): Accepts the alert, which is equivalent to clicking “OK” on the alert.

Other Alert Actions:

  • alert.dismiss(): Clicks “Cancel” on a confirmation dialog.
  • alert.send_keys(“text”): Sends text to a prompt dialog (if applicable).

Handling Checkboxes in Selenium Python

Checkboxes are commonly used in forms to let users select one or more options. Selenium makes it simple to locate, check, or uncheck these boxes using standard element interaction methods.

Working with Checkboxes

Selenium interacts with checkboxes using the click() method. Before clicking, it’s good practice to check whether the box is already selected to avoid unwanted toggling.

from selenium import webdriver

from selenium.webdriver.common.by import By




# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')




# Open a website with checkboxes (example URL)

driver.get("https://www.w3schools.com/howto/howto_css_custom_checkbox.asp")




# Locate the checkbox (adjust locator based on the actual HTML)

checkbox = driver.find_element(By.XPATH, "//input[@type='checkbox']")




# Check if it's already selected

if not checkbox.is_selected():

checkbox.click()  # Check the box




# Uncheck the box (if needed)

if checkbox.is_selected():

checkbox.click()  # Uncheck the box




# Close the browser

driver.quit()

Explanation:

  • find_element(By.XPATH, …): Locates the checkbox on the page.
  • is_selected(): Returns True if the checkbox is already checked.
  • click(): Toggles the checkbox on or off.

Other Checkbox Actions:

  • Use find_elements() to handle multiple checkboxes in a loop.
  • Add conditions to check or uncheck based on test logic.

Handling Cookies in Selenium Python

Cookies are small pieces of data stored by browsers to remember user sessions, preferences, or authentication tokens. Managing cookies is essential when testing login flows, user sessions, or personalized web content. Selenium provides simple APIs to read, add, delete, and manipulate cookies directly from your Python scripts.

1. Adding Cookies

You can add a cookie using a dictionary containing at least a name and value. Optional fields include path, domain, expiry, and secure.

from selenium import webdriver




driver = webdriver.Chrome()

driver.get("https://example.com")




cookie = {"name": "user", "value": "Rohit", "path": "/", "secure": True}

driver.add_cookie(cookie)

This allows you to simulate logged-in states or pre-set user preferences before loading a page.

2. Retrieving Cookies

To read cookies stored in the browser:

all_cookies = driver.get_cookies()  # Returns a list of dictionaries

print(all_cookies)



specific_cookie = driver.get_cookie("user")  # Returns a single cookie by name

print(specific_cookie)

This is useful for verifying session information or extracting data for debugging.

3. Deleting Cookies

Selenium lets you remove cookies selectively or clear all cookies:

driver.delete_cookie("user")  # Deletes a specific cookie

driver.delete_all_cookies()   # Deletes all cookies

This is helpful when testing logout flows or resetting sessions between tests.

Exception Handling in Selenium Python

When running Selenium tests, it’s common to encounter exceptions due to dynamic web content, slow page loads, or unexpected element behavior. Handling these exceptions properly ensures your scripts don’t fail abruptly and allows you to take corrective actions, improving reliability and maintainability.

Common exceptions in Selenium include: 

Cleanup and Teardown

Properly closing the browser session is crucial for releasing resources and ensuring that your automation script runs cleanly.

Properly Closing the Browser Session

Example: Closing the Browser

from selenium import webdriver



# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')



# Open a website

driver.get("https://www.python.org/")



# Perform actions (e.g., search)

search_bar = driver.find_element(By.NAME, "q")

search_bar.send_keys("Python")

search_bar.send_keys(Keys.RETURN)



# Cleanup: Close the browser

driver.quit()

Explanation:

  • driver.quit(): Closes all browser windows and ends the WebDriver session. This is the preferred method for cleanup as it ensures the browser process is terminated and resources are freed.

Alternative Methods:

  • driver.close(): Closes the current window. If it’s the only window open, it will end the session. Use driver.quit() for complete cleanup.

Handling Mouse Actions in Selenium Python

Modern web applications often use interactive elements that require advanced mouse actions like hovering, clicking, double-clicking, or dragging and dropping. Selenium handles these interactions using the ActionChains class.

Performing Mouse Actions

The ActionChains class in Selenium allows simulation of complex user gestures. These include hovering over elements, right-clicking, double-clicking and more.

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.action_chains import ActionChains




# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')




# Open a website with interactive elements (example URL)

driver.get("https://swisnl.github.io/jQuery-contextMenu/demo.html")




# Locate the element to interact with

button = driver.find_element(By.CSS_SELECTOR, ".context-menu-one")




# Create an ActionChains object

actions = ActionChains(driver)




# Right-click on the button

actions.context_click(button).perform()




# Hover over the element (if needed)

# actions.move_to_element(button).perform()




# Close the browser

driver.quit()

Explanation:

  • ActionChains(driver): Creates an object to define a chain of actions.
  • context_click(element): Performs a right-click on the specified element.
  • move_to_element(element): Moves the mouse pointer over an element (used for hover actions).
  • perform(): Executes the entire chain of actions.

Other Mouse Actions:

  • click(element): Performs a left-click.
  • double_click(element): Performs a double-click.
  • drag_and_drop(source, target): Drags an element and drops it onto another.

Locating WebElements in Selenium Python

Before interacting with elements on a web page such as clicking a button, entering text or reading content, Selenium needs to locate those elements accurately. This is done using locators, which are strategies to identify elements within the HTML structure.

Common Ways to Locate Elements

Selenium offers several methods to find elements using the By class from selenium.webdriver.common.by. Each method targets a different attribute or structure in the HTML.

from selenium import webdriver

from selenium.webdriver.common.by import By




# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')




# Open a sample web page

driver.get("https://www.google.com")




# Locate by Name (for Google search input field)

element_by_name = driver.find_element(By.NAME, "q")




# Locate by Tag Name (for Google search button)

element_by_tag = driver.find_element(By.TAG_NAME, "input")




# Locate by Link Text (for the "Google Search" link on Google's footer)

element_by_link_text = driver.find_element(By.LINK_TEXT, "Google")




# Locate by Partial Link Text (for part of the footer link)

element_by_partial_link = driver.find_element(By.PARTIAL_LINK_TEXT, "Search")




# Locate by CSS Selector (for the search input box)

element_by_css = driver.find_element(By.CSS_SELECTOR, "input[name='q']")




# Locate by XPath (for the search button)

element_by_xpath = driver.find_element(By.XPATH, "//input[@name='btnK']")




# Close the browser

driver.quit()

Explanation:

  • By.ID: Targets an element with a specific id.
  • By.NAME: Targets an element with a given name attribute.
  • By.CLASS_NAME: Targets an element using the class value.
  • By.TAG_NAME: Useful for finding elements like <button>, <input>, or <a>.
  • By.LINK_TEXT and By.PARTIAL_LINK_TEXT: Used to locate links by visible text.
  • By.CSS_SELECTOR: A powerful method to find elements using CSS rules.
  • By.XPATH: Allows precise and complex element selection using XML-like path expressions.

Other Locator Tips:

  • find_element() returns a single match.
  • find_elements() returns a list of matches, useful for handling multiple similar elements.

Handling iFrames in Selenium Python

Some web pages include content embedded inside inline frames (iFrames). These iFrames act like separate HTML documents within the main page. To interact with elements inside an iFrame, Selenium must first switch its focus to that frame.

Working with iFrames

Selenium provides methods to switch to an iFrame before accessing its elements. Once done, it can switch back to the main content using switch_to.default_content().

from selenium import webdriver

from selenium.webdriver.common.by import By




# Set up the WebDriver

driver = webdriver.Chrome('./chromedriver')




# Open a page with an iframe

driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml_iframe")




# Switch to the iframe by name or ID

driver.switch_to.frame("iframeResult")




# Locate an element inside the iframe

paragraph = driver.find_element(By.TAG_NAME, "p")

print("Paragraph text inside iframe:", paragraph.text)




# Switch back to the main page content

driver.switch_to.default_content()




# Close the browser

driver.quit()

Explanation:

  • switch_to.frame(“name_or_id”): Switches Selenium’s focus to the specified iFrame using its name or id.
  • switch_to.frame(index): Switches to an iFrame based on its position (0 for the first one).
  • switch_to.frame(webelement): Switches using a reference to the actual frame element.
  • switch_to.default_content(): Returns control to the main page outside the iFrame.

Other iFrame Handling Tips:

  • Always switch to the correct frame before interacting with inner elements.
  • Use driver.find_elements(By.TAG_NAME, “iframe”) to list all frames on a page.
  • Be cautious of nested iFrames. Multiple switch_to.frame() calls may be needed.

Page Object Model (POM) in Selenium Python

The Page Object Model (POM) is a design pattern used in Selenium to create modular, maintainable, and reusable test code. Instead of writing locators and test logic together, POM separates the web page structure from the test scripts. Each page of the application is represented by a class, encapsulating all elements and actions that can be performed on that page.

Using POM brings several advantages:

  • Readability: Tests are easier to understand because page interactions are abstracted into meaningful methods.
  • Maintainability: When UI changes, only the page class needs updating, not all test scripts.
  • Reusability: Common page actions can be reused across multiple tests, reducing duplication.

To implement POM in Selenium Python: 

1. Create a Page Class

Each page class represents a single web page. Locators and methods are encapsulated in the class.

from selenium.webdriver.common.by import By




class LoginPage:

    def __init__(self, driver):

        self.driver = driver

        self.username_input = (By.ID, "username")

        self.password_input = (By.ID, "password")

        self.login_button = (By.ID, "loginBtn")




    def enter_username(self, username):

        self.driver.find_element(*self.username_input).send_keys(username)




    def enter_password(self, password):

        self.driver.find_element(*self.password_input).send_keys(password)




    def click_login(self):

        self.driver.find_element(*self.login_button).click()

2. Create Test Scripts Using Page Objects

from selenium import webdriver

from login_page import LoginPage




driver = webdriver.Chrome()

driver.get("https://example.com/login")




login_page = LoginPage(driver)

login_page.enter_username("testuser")

login_page.enter_password("password123")

login_page.click_login()

This keeps the test script clean and readable, focusing on what the test does, not how elements are located.

Testing Framework Integration

Integrating Selenium tests with a testing framework provides structured test cases, reporting, and additional functionality such as setup and teardown methods.

1. Integrate with unittest Framework

unittest is a built-in Python testing framework that provides a structured approach to writing and running tests, including test case management, fixtures, and test discovery.

Integrating Selenium with unittest allows for organized test cases, setup and teardown methods, and detailed test reports, making it easier to manage and maintain automated tests.

Example: Basic Test with unittest

import unittest

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys



class PythonOrgSearchTest(unittest.TestCase):



    @classmethod

    def setUpClass(cls):

        cls.driver = webdriver.Chrome('./chromedriver')

        cls.driver.get("https://www.python.org/")



    def test_search_python(self):

        search_bar = self.driver.find_element(By.NAME, "q")

        search_bar.send_keys("Python")

        search_bar.send_keys(Keys.RETURN)

        self.assertIn("Python", self.driver.title)



    @classmethod

    def tearDownClass(cls):

        cls.driver.quit()



if __name__ == "__main__":

    unittest.main()

Explanation:

  • unittest.TestCase: Defines a test case class. Each method within the class represents a test case.
  • setUpClass(): Initializes resources needed for the tests. Runs once before any test methods are executed.
  • tearDownClass(): Cleans up resources. Runs once after all test methods have completed.
  • unittest.main(): Runs the tests and provides output in the console.

2. Integrate with pytest Framework

pytest is a powerful and flexible Python testing framework that simplifies writing tests with its rich feature set, including fixtures, parameterized tests, and detailed assertions.

Integrating Selenium with pytest enhances test organization, facilitates advanced setup/teardown functionality, and generates comprehensive test reports, improving test reliability and clarity.

Example: Basic Test with pytest

import pytest

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys


@pytest.fixture(scope="module")

def driver():

driver = webdriver.Chrome('./chromedriver')

yield driver

driver.quit()


def test_search_python(driver):

driver.get("https://www.python.org/")

search_bar = driver.find_element(By.NAME, "q")

search_bar.send_keys("Python")

search_bar.send_keys(Keys.RETURN)

assert "Python" in driver.title

Explanation:

  • pytest.fixture(): Defines a fixture that sets up and tears down resources. The scope=”module” ensures the fixture is run once per module.
  • yield: Provides the driver instance to the test function and performs cleanup after the test completes.
  • assert: Checks that the condition is met. pytest will report the assertion failure if the

Run Selenium Python Tests

Why Run Selenium Python Tests on BrowserStack Real Device Cloud?

Testing on local machines or simulators can give an initial sense of functionality, but it often fails to capture real-world behavior. Real devices expose differences in performance, rendering, touch interactions, and browser quirks that simulators or desktop setups cannot replicate.

Moreover, features like network latency, device-specific bugs, font rendering, and input methods often behave differently on actual hardware.

Platforms like BrowserStack Real Device Cloud solve this problem by providing instant access to a wide range of real devices and browsers.

You can run Selenium Python scripts across multiple devices, OS versions, and browser combinations without managing hardware or complex setups, ensuring your tests reflect true user experiences.

Here are core features of BrowserStack that support Selenium Python testing:

  • Selenium Grid on Cloud: Run Selenium tests on actual devices and browsers for accurate, real-world results without managing hardware.
  • Cross-Browser Testing: Verify your app works smoothly across multiple browsers and operating systems.
  • Parallel Testing: Run multiple tests simultaneously on different devices and browsers to save time.
  • Simplified Debugging: Get videos, screenshots, and logs to quickly identify and fix issues.
  • CI/CD Integration: Seamlessly integrate with Jenkins, CircleCI, GitHub Actions, and automate tests as part of your pipeline.

Struggling with Selenium Python?

Talk to QA experts who have been in your shoes and get advice on setup, best practices etc.

Conclusion

Selenium with Python allows you to automate web applications efficiently. Use POM to structure your code, handle cookies to manage sessions, manage exceptions to prevent test failures, and run cross-browser tests to ensure your application works reliably across environments.

Additionally, use BrowserStack to access multiple browsers and OS instantly, run tests in parallel, and leverage logs, screenshots, and videos for faster debugging. Integrate it into your CI/CD pipeline to ensure your tests reflect true user experiences and deliver high-quality releases faster.

Try BrowserStack Now

Useful Resources for Selenium and Python

Tags
Python Selenium
Not sure on Selenium - Python Testing?
Get expert guidance to master Selenium with Python and automate web testing efficiently.

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord