Run Selenium Python Tests on Real Devices

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

Get Started free
Home Guide Selenium Python Tutorial (with Example)

Selenium Python Tutorial (with Example)

By Shaumik Daityari, Community Contributor -

New features are regularly added to web applications to boost user engagement. To ensure these updates work as intended and that the user interface remains functional, automated testing is crucial. Selenium is a widely-used tool for this type of automation testing.

Selenium is an open-source automation testing tool that supports various scripting languages such as C#, Java, Perl, Ruby, JavaScript, and others. The choice of scripting language can be made based on the specific requirements of the application being tested.

Python is one of the most popular choices when it comes to scripting with 51% of the developers using it, as suggested by the StackOverflow 2024 annual survey.

Why do Developers prefer Python for writing Selenium Test Scripts?

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

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.

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()

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.

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

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

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

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.

Navigate through Windows and Frames

When working with multiple browser windows or tabs, or dealing with iframes (frames), you may need to switch contexts to interact with different elements.

Step 1. Handling Multiple Browser Windows or Tabs

Example: Switching Between Windows

To handle multiple browser windows or tabs:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import time



# Set up the WebDriver

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



# Open the Python website

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



# Open a new tab with a different URL

driver.execute_script("window.open('https://www.google.com', '_blank');")



# Switch to the new tab

driver.switch_to.window(driver.window_handles[1])



# Perform actions in the new tab (e.g., search for 'Selenium')

search_bar = driver.find_element_by_name("q")

search_bar.clear()

search_bar.send_keys("Selenium")

search_bar.send_keys(Keys.RETURN)



# Switch back to the original tab

driver.switch_to.window(driver.window_handles[0])



# Close the browser

driver.quit()

Explanation:

  • window_handles: Retrieves a list of window handles. Switch to a specific window using switch_to.window().
  • execute_script(“window.open()”): Opens a new tab or window.

Step 2. Switching Between Frames

Example: Switching to an iFrame

To switch to and interact with elements within an iframe:

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

import time



# Set up the WebDriver

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



# Open the Python website

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



# Example site with iframe (replace with an actual URL that contains iframes)

driver.get("https://www.w3schools.com/html/html_iframe.asp")



# Switch to the iframe using its name or ID

driver.switch_to.frame("iframeResult")



# Perform actions within the iframe

print(driver.find_element_by_tag_name("h1").text)



# Switch back to the default content

driver.switch_to.default_content()



# Close the browser

driver.quit()

Explanation:

  • switch_to.frame(): Switches to a specific iframe.
  • switch_to.default_content(): Switches back to the main page.

Handling Waits

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).

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.

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 on Real Devices

Selenium Python Resources

Best Practices using Selenium WebDriver with Python

Here are five best practices for using Selenium WebDriver with Python:

  1. Use Explicit Waits: Prefer explicit waits over implicit waits to handle dynamic content. Explicit waits ensure that your script interacts with elements only when they are ready, reducing the chances of encountering timing issues.
  2. Organize Tests with Frameworks: Integrate Selenium tests with testing frameworks like unittest or pytest to structure your test cases, manage setup and teardown, and generate detailed test reports.
  3. Use Page Object Model (POM): Implement the Page Object Model to separate test logic from page-specific code. This design pattern promotes code reusability, maintainability, and easier updates.
  4. Handle Exceptions Carefully: Implement error handling and logging to manage unexpected situations, such as element not found or timeout errors. This helps in debugging and provides insights into test failures.
  5. Optimize Browser Performance: Run tests in headless mode or use browser profiles to speed up test execution and reduce resource consumption. Also, ensure that browser drivers are up-to-date for compatibility and performance improvements.

BrowserStack Automate Banner 5

Why run Selenium Python Tests on BrowserStack Real Device Cloud?

Running Selenium Python tests on BrowserStack’s Real Device Cloud offers numerous advantages that significantly enhance testing efficiency and effectiveness.

BrowserStack provides access to a wide range of real devices and browsers, ensuring that tests reflect real-world scenarios and uncover device-specific issues. The platform supports scalable parallel execution, allowing multiple tests to run simultaneously across various configurations, which accelerates the development cycle.

Cross-platform testing on BrowserStack ensures consistent application performance across different environments. Additionally, it offers real-time debugging features such as live logs, screenshots, and video recordings, which aid in quick troubleshooting.

Seamless integration with CI/CD pipelines further automates the testing process, enabling tests to run on every code change and providing immediate feedback on application quality. Overall, BrowserStack Automate enables comprehensive, efficient, and reliable testing, fostering continuous development and deployment.

Talk to an Expert

Tags
Python Selenium

Featured Articles

How to Create and Use Action Class in Selenium Python

How to Double Click on an Element in Selenium Python?

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers