Selenium With Python
Run your Python Selenium tests on the TestMu AI cloud grid across 10,000+ browser/device combinations. The setup is the same for every framework: you connect to the grid and pass your capabilities. This guide covers that shared flow once, then gives you a per-framework quickstart in the tabs below.
Prerequisites
Complete these steps before running your first Python Selenium test.
-
Install the latest Python build from the official website.
-
Verify that pip is installed in your system. Install pip from pip documentation.
-
Download the latest Selenium Client and its WebDriver bindings from the official website.
-
Create a TestMu AI account if you don't have one.
-
Get your Username and Access Key from the TestMu AI Dashboard.
-
Install Python and pip.
-
Install the Selenium client and WebDriver bindings, and (recommended)
virtualenvto isolate dependencies.
To create and activate a virtual environment on macOS or Linux:
Verifiedpip install virtualenv
virtualenv venv
source venv/bin/activate
On Windows, activate it with venv\Scripts\activate instead.
Set Your Credentials
Clone the repository and install dependencies.
Set TestMu AI Username and Access Key in environment variables.
git clone https://github.com/LambdaTest/python-selenium-sample
cd python-selenium-sample
Install the Selenium driver using pip:
Verifiedpip install selenium
export PYTHONWARNINGS="ignore:Unverified HTTPS request" //Disable ssl warning
Step 2: Set Your Credentials
Configure your credentials to connect to the TestMu AI Selenium Grid.
Set TestMu AI Username and Access Key in environment variables.
- macOS / Linux
- Windows
export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"
set LT_USERNAME="undefined"
set LT_ACCESS_KEY="undefined"
Step 3: Configure Your Test Capabilities
Define browser, version, and OS settings for your test run.
In the Python script, update your test capabilities. This code passes browser, browser version, and operating system information, along with TestMu AI Selenium grid capabilities via the capabilities object.
Verifiedcapabilities = {
"build": "your build name",
"name": "your test name",
"platformName": "Windows 10",
"browserName": "Chrome",
"browserVersion": "latest",
}
Use the TestMu AI Capabilities Generator to auto-generate the capabilities class for your test requirements.
Step 4: Run the Test
Execute the Python Selenium test from the command line.
The selenium-skill is part of TestMu AI Agent Skills - structured packages that teach AI coding assistants how to write production-grade test automation.
python google-search-lambdatest.py
Step 5: View Your Results
Check the test output on the console and the TestMu AI dashboard.
Visit the TestMu AI Automation Dashboard to view your test results. The dashboard provides:
- Text logs for each test step
- Screenshots captured during execution
- Video recordings of the full test session
Run Python Selenium Tests Using Agent Skills
Use AI coding assistants to generate and run Python Selenium tests with the TestMu AI Agent Skill.
The selenium-skill is part of TestMu AI Agent Skills - structured packages that teach AI coding assistants how to write production-grade test automation.
Install the skill:
Verifiedgit clone https://github.com/LambdaTest/agent-skills.git
cp -r agent-skills/selenium-skill .claude/skills/
# For Cursor / Copilot
cp -r agent-skills/selenium-skill .cursor/skills/
Install all available framework skills at once by cloning the repository directly into your tool's skills directory (e.g., .claude/skills/, .cursor/skills/).
Run a Test in Your Framework
Each tab lists the framework-specific pieces. Clone the matching repo (it contains the full, ready-to-run project), then run.
- unittest
- pytest
- Robot
- Behave
- Lettuce
The standard-library unittest framework connects a remote WebDriver to the grid, with capabilities inline in the test.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/Python-UnitTest-Selenium
cd Python-UnitTest-Selenium
- Set your browser and OS in the capabilities dictionary:
capabilities = {
"build": "UnitTest-Selenium-Sample",
"name": "UnitTest-Selenium-Test",
"platformName": "Windows 11",
"browserName": "Chrome",
"browserVersion": "latest",
}
- Install the dependencies:
pip install -r requirements.txt
- Run the test:
python lambdatest_test.py
The test then appears on the Automation Dashboard. A green status confirms it passed.
pytest keeps capabilities in conftest.py and runs in parallel via pytest-xdist.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/pytest-selenium-sample
cd pytest-selenium-sample
- Set your browser and OS in
conftest.py:
capabilities = {
"build": "Sample PY Build",
"platformName": "Windows 11",
"browserName": "Chrome",
"browserVersion": "latest",
}
- Install the dependencies:
pip install -r requirements.txt
- Run the test. The actual pytest run is the
pytestcommand (which also enables parallel execution); the plainpythoninvocation runs the script directly:
python tests/lt_sample_todo.py
pytest -s -n=2 tests/lt_sample_todo.py
The test then appears on the Automation Dashboard. A green status confirms it passed.
Robot Framework uses Selenium2Library, with capabilities and the grid URL declared as variables in a .robot file, run through a Makefile.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/Robot-Selenium-Sample
cd Robot-Selenium-Sample
- Review the browser and grid settings in
Resources/Common.robot. The browser comes from theROBOT_BROWSERandbrowserNamevalues that the Makefile passes in, and the credentials are read from yourLT_USERNAMEandLT_ACCESS_KEYenvironment variables:
*** Settings ***
Library SeleniumLibrary
Library LambdaTestStatus.py
*** Variables ***
${BROWSER} ${ROBOT_BROWSER}
&{lt_options} browserName=${browserName} name=RobotFramework Lambda Test buildName=Robot Build
${REMOTE_URL} http://%{LT_USERNAME}:%{LT_ACCESS_KEY}@hub.lambdatest.com/wd/hub
${TIMEOUT} 30000
${IMPLICIT_WAIT} 9seconds
*** Keywords ***
Open test browser
[Timeout] ${TIMEOUT}
${options}= Evaluate sys.modules['selenium.webdriver'].${BROWSER}Options() sys, selenium.webdriver
Call Method ${options} set_capability LT:Options ${lt_options}
Open Browser https://www.testmuai.com/selenium-playground/todo-app/
... browser=${BROWSER}
... remote_url=${REMOTE_URL}
... options=${options}
Set Selenium Implicit Wait ${IMPLICIT_WAIT}
Close test browser
Run keyword if '${REMOTE_URL}' != ''
... Report Lambdatest Status
... ${TEST_NAME}
... ${TEST_STATUS}
Close All Browsers
- Install the dependencies:
pip install -r requirements.txt
- Run a single test, or all in parallel:
make test_Windows_10_chrome_latest
make run_all_in_parallel
The test then appears on the Automation Dashboard. A green status confirms it passed.
Behave is BDD for Python: feature files plus step definitions, with capabilities in a JSON config.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/Python-Behave-Selenium
cd Python-Behave-Selenium
- Set your browser and OS in
config/config.json:
[
{
"platformName": "Windows 10",
"browserName": "chrome",
"browserVersion": "latest",
"build": "Behave Selenium Sample",
"name": "Behave Sample Test"
}
]
- Install the dependencies:
pip install -r requirements.txt
- Run the test:
behave features/test.feature
The test then appears on the Automation Dashboard. A green status confirms it passed.
Lettuce is unmaintained (since 2016, Python 2 only). For new projects, use the actively maintained Behave or pytest frameworks instead (see the Behave and pytest tabs).
Lettuce is a BDD framework for Python: feature files plus step definitions, with capabilities in a JSON config.
- Clone the sample GitHub project:
git clone https://github.com/LambdaTest/sample-lettuce
cd sample-lettuce
-
Set your browser and OS in
config.json. -
Install the dependencies:
pip install -r requirements.txt
- Run the test:
python tests/lt_sample_todo.py
The test then appears on the Automation Dashboard. A green status confirms it passed.
View Your Results
Your test results, including video, network logs, and command-by-command execution, appear on the TestMu AI Automation Dashboard.
Next steps: If this is your first run, walk through running your first Selenium test end to end. From there, explore the full set of Selenium automation capabilities you can pass to the grid, learn how to debug your Selenium tests, and organize and filter your Selenium tests as your suite grows.
Next Steps
Continue with these related guides:
