BrowserStack Python SDK (Legacy)
This is a legacy Python SDK for integrating Selenium-webdriver tests with BrowserStack. It is officially deprecated and no longer actively maintained. BrowserStack now recommends using standard Selenium with BrowserStack capabilities directly. The last release of this SDK was 1.45.0 in April 2022.
Common errors
-
ModuleNotFoundError: No module named 'browserstack'
cause Attempting to import modules from the deprecated `browserstack-sdk` without it being installed, or after migrating to the modern approach that doesn't use these imports.fixIf you intend to use the legacy SDK (not recommended), run `pip install browserstack-sdk`. Otherwise, remove all imports related to `browserstack-sdk` (e.g., `from browserstack.local import Local`) and rewrite your test using standard `selenium` methods with direct capability configuration. -
selenium.common.exceptions.WebDriverException: Message: Error forwarding the new session Error forwarding the new session Request: {}cause This generic WebDriver error often indicates issues with BrowserStack credentials, outdated capabilities, or problems connecting to the BrowserStack hub. When using the deprecated SDK, it might not correctly handle newer authentication or connection methods.fixVerify `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY` environment variables (or hardcoded values) are correct. Ensure your specified browser and OS capabilities are valid and supported by BrowserStack. If using local testing, verify the BrowserStack Local service is running and configured correctly according to the *latest* BrowserStack documentation, not the SDK's old methods. -
AttributeError: 'Remote' object has no attribute 'get_bstack_local'
cause This SDK might have provided helper methods or custom WebDriver wrappers that are no longer available or relevant when using standard `selenium.webdriver.Remote`. Such an error indicates a call to a method specific to the SDK that does not exist on the standard Selenium WebDriver object.fixIf you are migrating away from the deprecated SDK, replace such SDK-specific calls with standard Selenium WebDriver methods or direct BrowserStack capability settings. If you *must* use the SDK, ensure you are calling methods correctly as per its original documentation and that you are using the correct WebDriver instance that the SDK might have wrapped.
Warnings
- breaking The `browserstack-sdk` library is officially deprecated and no longer actively maintained by BrowserStack. It is strongly recommended to use standard `selenium` with BrowserStack capabilities directly for all new development.
- gotcha Using this deprecated SDK may lead to compatibility issues with newer versions of Python, Selenium, or BrowserStack infrastructure, as it receives no further updates. This could result in unexpected errors or security vulnerabilities.
- gotcha The methods for setting up local testing (BrowserStack Local) provided by `browserstack.local.Local` are specific to this deprecated SDK. The recommended approach for local testing now involves setting `bstack:options` in your capabilities and typically starting the local tunnel via the BrowserStack Local application or CLI, not through this SDK.
Install
-
pip install browserstack-sdk
Imports
- Local
from browserstack.local import Local
Quickstart
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from browserstack.local import Local # Specific to the DEPRECATED SDK
# --- WARNING: This SDK is deprecated. Use standard Selenium setup for new projects. ---
# Configuration using environment variables
BROWSERSTACK_USERNAME = os.environ.get('BROWSERSTACK_USERNAME', 'YOUR_USERNAME')
BROWSERSTACK_ACCESS_KEY = os.environ.get('BROWSERSTACK_ACCESS_KEY', 'YOUR_ACCESS_KEY')
# Desired capabilities for the test
desired_cap = {
'bstack:options': {
'userName': BROWSERSTACK_USERNAME,
'accessKey': BROWSERSTACK_ACCESS_KEY,
'buildName': 'BrowserStack Python SDK Example (Legacy)',
'sessionName': 'Local Test with Deprecated SDK',
'local': 'true' # Enable local testing
},
'browserName': 'Chrome',
'browserVersion': 'latest',
'os': 'Windows',
'osVersion': '10'
}
bs_local = None
driver = None
try:
# Start BrowserStack Local using the deprecated SDK's Local class
bs_local = Local()
bs_local_args = {
"key": BROWSERSTACK_ACCESS_KEY,
"forcelocal": "true"
}
print("Starting BrowserStack Local tunnel...")
bs_local.start(bs_local_args)
print("BrowserStack Local tunnel started.")
# Initialize Selenium WebDriver connected to BrowserStack hub
driver = webdriver.Remote(
command_executor='https://hub.browserstack.com/wd/hub',
desired_capabilities=desired_cap
)
# Perform a test that requires local access
driver.get("http://bs-local.com:45454/")
print(f"Page title: {driver.title}")
# Example assertion (this specific page is part of BrowserStack Local setup)
if "BrowserStack Local" in driver.title:
print("Test Passed: Successfully accessed local URL through BrowserStack Local.")
else:
print("Test Failed: Could not access local URL.")
finally:
# Stop BrowserStack Local tunnel
if bs_local:
print("Stopping BrowserStack Local tunnel...")
bs_local.stop()
print("BrowserStack Local tunnel stopped.")
# Quit the WebDriver session
if driver:
driver.quit()
print("WebDriver session quit.")
# To run this example:
# 1. Set BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.
# 2. pip install browserstack-sdk selenium
# 3. python your_script.py