Skip to main content

Selenium Python:Run Python Automation Testing Scripts on Cloud Selenium Grid

LambdaTest Selenium Automation Grid is a cloud-based scalable Selenium testing platform which enables you to run your automation scripts on 2000+ different browsers and operating systems. You can now run your Python Selenium automated test cases on a scalable Selenium infrastructure that is running real browsers and real operating systems.

This post will help you in getting started with configuring and running your Python-based automation test scripts on LambdaTest Selenium cloud platform. In this post we would be exploring:

  1. How to run a sample Selenium with Python automation testing on LambdaTest.
  2. How to set up an environment for Python automation testing your locally hosted websites or web pages.
  3. How to configure Python automation testing desired capabilities for executing a Selenium test.
  4. How to run your Selenium Python test cases in parallel to reduce build times.
  5. Advanced features of LambdaTest.

Prerequisites For Running Selenium Python Scripts#

All the code samples in this documentation can be found in the Python LambdaTest Repository on Image GitHub. You can either download or clone the repository to quickly run your tests.

  • Have Selenium WebDriver client driver installed inside your local Python automation testing environment.
  • Make sure that you have the latest Selenium Python build installed. You can download the latest Python build from
  • You would need Python package manager, pip, as well. Usually latest python automation testing framework installers come prepacked with pip. However if you don’t have pip You can install it using command:
curl | python
  • Download the driver from the link, or you can use pip to install it.
pip install selenium
  • If you wish to test locally hosted or privately hosted projects, you would also need LambdaTest Tunnel binary file.

Getting Started With Python Automation Testing on LambdaTest#

The first step in using LambdaTest platform is to understand LambdaTest’s Selenium Grid capabilities. Our Selenium Grid uses remote webdriver instead of normal Selenium client browser drivers so if you are migrating from locally run Selenium, you would have to invoke LambdaTest Selenium remote webdriver. Next you need to specify in your code, which browser, browser versions, operating systems, and resolution you wish to run your test on, along with defining LambdaTest specific capabilities. You can checkout LambdaTest Capabilities Generator tool to understand more about how you can define running browser environments and leverage advanced LambdaTest capabilities.

Let’ start with a simple Selenium Remote Webdriver test first. The test automation script below tests a simple to-do application with basic functionalities like mark items as done, add items in a list, calculate total pending items etc. You can also find this at our Git repository Python-UnitTest-Selenium.

Example of the Base Test#

You can observe that basics for setting up a test to run on LambdaTest is simple.

 from selenium import webdriverimport unittestimport osfrom configparser import ConfigParser caps={} class BaseTest(unittest.TestCase):    # ======================================================================    #            Setup and tear down functions    # ======================================================================     @classmethod    def setUpClass(self):         ROOT_DIR = os.path.dirname(os.path.abspath(__file__))         self.config = ConfigParser()         my_file = ROOT_DIR + '\config.cfg'         self.init_capabilities(self)         self.base_url = ""               remote_url= "https://"+self.user_name+":"+self.app_key+""         caps['name'] = "LambdaTestSampleApp"         caps['build'] = "PythonUnitTestSample"         caps['browserName'] = self.browser               caps['version'] = self.browser_version         caps['platform'] = self.os         self.driver = webdriver.Remote(command_executor=remote_url,desired_capabilities=caps)         self.driver.implicitly_wait(30)         self.driver.maximize_window()         # open application url         self.driver.get(self.base_url)     @classmethod     def tearDownClass(self):         print ( "closed browser" )         self.driver.quit()     def init_capabilities(self):         print ("Preparing capabilities")               if os.getenv('LT_USERNAME', 0) == 0:             self.user_name = self.config['Environment']['UserName']         if os.getenv('LT_APPKEY', 0)  == 0:              self.app_key = self.config['Environment']['AppKey']         if os.getenv('LT_OPERATING_SYSTEM', 0) == 0:              self.os = self.config['Environment']['OS']         if os.getenv('LT_BROWSER', 0)  == 0:              self.browser = self.config['Environment']['Browser']         if os.getenv('LT_BROWSER_VERSION', 0)  == 0:              self.browser_version = self.config['Environment']['BrowserVersion']

Code Example#

import unittest, time, reimport base_test from selenium.webdriver.common.keys import Keys class LambdaTest(base_test.BaseTest):     def test_unit_user_should_able_to_add_item(self):         # try:         driver = self.driver         # Url         driver.get(self.base_url)         # Click on check box         check_box_one = driver.find_element_by_name("li1")         # Click on check box         check_box_two = driver.find_element_by_name("li2")         # Enter item in textfield         textfield = driver.find_element_by_id("sampletodotext")         textfield.send_keys("Yey, Let's add it to list")         # Click on add button         add_button = driver.find_element_by_id("addbutton")         # Verified added item         added_item = driver.find_element_by_xpath("//span[@class='done-false']").text         print (added_item)         self.assertEqual ("Yey, Let's add it to list", added_item )         if added_item in "":             return True         else:             return False  if __name__ == "__main__":     unittest.main()

If you want to run the test remotely on the LambdaTest.

capabilities = {    "build" : "your build name",    "name" : "your test name",    "platform" : "Windows 10",    "browserName" : "Chrome",    "version" : "71.0",}

Execute The Test#

You would need to execute the below command in your terminal/cmd.


Understanding & Configuring Selenium Python Desired Capabilities#

As we said earlier the first step is to configure your test scripts to connect with LambdaTest Selenium automation gird. In the above mentioned Python automation testing code, the first thing you would notice is the invoking of remote webdriver instead of the native browser webdrivers. So for example if you are planning to run on Firefox browser in your local machine, you would be using Firefox browser driver like this:

FirefoxDriver driver = new FirefoxDriver();

However to run on LambdaTest Selenium grid, you would have to change it remote webdriver and at the same time pass capabilities related to browser, browser versions etc. Here is how it would look like:

self.driver = webdriver.Remote(command_executor="https://USERNAME:[email protected]/wd/hub",desired_capabilities=caps)

You can define capabilities for invoking a specific Remote Web Chrome Driver using the below command.

capabilities = {    "chrome.driver" : 2.42    }

Our Capabilities Generator will automatically provide you the program, based on your selections of test configurations. For Instance, if you pick the below configurations:

Operating SystemWindows 10
BrowserGoogle Chrome
Browser Version71.0
Selenium Version3.13.0

Then, LambdaTest Capabilities Generator will automatically generate the below program:

capabilities = {        "build" : "your build name",        "name" : "your test name",        "platform" : "Windows 10",        "browserName" : "Chrome",        "version" : "71.0"    }

The most important capabilities to understand here are ‘browserName’, ‘versions’, and ‘platform’. They define which browser environment you wish to run the test on. Rest of the capabilities are important in test management and debugging. We have a inbuilt capabilities generator tool as well that you use to generate capabilities code for your test suite.

You can also check out our documentation on capabilities generator to understand more about individual capabilities.

Advanced LambdaTest Capabilities#

In addition to default Selenium Grid capabilities, LambdaTest also has platform-specific capabilities like a video recording of test runs, console logs of each test run, network logs of each test run, custom profiles for specific browsers, etc. Do check out our documentation on LambdaTest Advanced capabilities as well.

Testing Locally Hosted or Privately Hosted Projects using Selenium Python#

To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name LambdaTest Tunnel. With LambdaTest Tunnel, you can test your locally hosted files before you make them live over the internet. You could even perform cross browser testing from different IP addresses belonging to various geographic locations. You can also use LambdaTest Tunnel to test web-apps and websites that are permissible inside your corporate firewall. The SSH tunnel provided by LambdaTest acts as a proxy server for hosting your web pages from your local machine to Virtual machines running on LambdaTest cloud servers. Curious to know more about LambdaTest Tunnel?

Follow our documentation on LambdaTest Tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.

Download the binary file of:

You can start tunnel by executing the below command in your cmd(Command Prompt) / terminal. Make sure that your pointer is routed in the same directory where you extract the downloaded LambdaTest Tunnel zip file.

LT -user [user's login email] -key [user's access key]

So for example, if your user login email is [email protected] and your user key is 123asd123, then the command would be:

LT -user [email protected] -key 123asd123

Once, the tunnel is successfully set up. You can add the below code to your capabilities for testing internal servers on your network.

capabilities = {    "tunnel" : true,}

You can also add the capability using LambdaTest Capabilities Generator.

Tunnel Reference: For a full reference of all tunnel features we support, visit our tunnel documentation.

Mark Selenium Python Tests As Pass Or Fail#

While performing automation testing using Selenium Python on LambdaTest Selenium grid, it is imperative to flag an automation test as either pass or fails. This is completely dependent on your testing requirement with respect to the validation of expected behaviour. You can mark whether an automation test passed or failed by adding the below code fragment in your automation script:


Queuing Selenium Python Tests#

To prevent over-exploitation of the platform we have added a capacity constraint on the number of Selenium Python tests that can be queued at our platform. Your maximum queue capacity will be dependent upon the number of concurrent sessions your LambdaTest account is eligible for. Below formula will help you calculate the maximum capacity for your queue.

Maximum Queuing Capacity Maximum number of parallel tests that can be queued = n + 150 Here, n = number of concurrent sessions. For example, if you have opted for pricing where you can avail up to 10 concurrent sessions. Then the maximum number of parallel test cases that can be queued will be equal to 10 + 150 i.e. 160 parallel test cases. Queuing allows you to manage your test pipeline in a hassle-free manner. LambdaTest would automatically schedule and execute test cases for you.

LambdaTest Queuing Policy

Here you can get more information about LambdaTest Queuing Policy.

Queuing Timeout
There is also a limit on how long a test case can stay in the queue. If your test case stays in the queue for more than 15 minutes, the test case would be timed out and would not be executed.

Last updated on