Selenium with Python

Your guide to running Selenium Webdriver tests with Python on BrowserStack.


BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with Python on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Mark tests as pass / fail
  3. Debug your app


Before you can start running your Selenium tests with Python, ensure the following libraries are installed:

# If pip is not install, you can install it using:
sudo easy_install pip
# If you prefer pip, then use the following command:
sudo pip install selenium

Run your first test

Note: Running your Selenium tests on BrowserStack requires a BrowserStack Username and Access Key.

To obtain your username and access keys, sign up for a Free Trial or purchase a plan.

To get started, let’s run a simple Selenium Webdriver test. The Python script below will open a URL, input a string, submit the form, and return the page title.

First, select the OS and Device/Browser combination you’d like to test on using the drop-down menus below. This will automatically update the Python code sample below:

Note: Testing on real devices requires the Automate Mobile plan

Look for the icon to select a real device.


Warning: The driver.quit() statement is required, otherwise the test continues to execute, leading to a timeout.

Second, copy-and-paste the code sample into your code editor, save it as a .py file, and execute the test from your command line.

Third, verify the results. The Selenium Webdriver test should have opened a URL, inputted a string, submitted the form, and returned the page title. Your results will be displayed on the command-line interface and on the Automate dashboard, where you can see Text Logs, Screenshots of every Selenium command, and a Video Recording of your entire test.

Mark tests as pass / fail

It is possible to mark tests as either a pass or a fail, using the following snippet:

import requests
requests.put('<session-id>.json', data={"status": "passed", "reason": ""})

The two potential values for status can either be passed or failed. Optionally, a reason can also be passed.

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

  • Text Logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

  • Visual Logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your Python script. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true:

desired_cap['browserstack.debug'] = True
  • Video Recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording increases test execution time slightly. You can disable this feature by setting the capability to false.

desired_cap[''] = False

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. Complete details to enable all the debugging options can be found here

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked

Thank you for your valuable feedback