SeleniumBase
SeleniumBase is a complete web automation framework for end-to-end testing, built on top of the Selenium/WebDriver APIs. It simplifies browser automation with an intuitive syntax, built-in features for reliable interactions, and advanced capabilities like bot-detection avoidance. It supports various tasks from testing to web scraping and maintains a rapid release cadence with frequent patches.
Warnings
- gotcha SeleniumBase methods (`self.open()`, `sb.type()`, etc.) must be called within the `BaseCase` test class context (using `self`) or the `SB()` context manager instance. Calling them directly or outside this scope will result in errors like 'It looks like you are trying to call a SeleniumBase method from outside the scope of your test class's `self` object'.
- gotcha Element detection can be less reliable in headless mode compared to visible browser mode, leading to 'Element Not Found' errors due to differences in page rendering or dynamic content loading. This is a common challenge with headless browser automation.
- gotcha Web scraping scripts using SeleniumBase can break unexpectedly if the target website's HTML structure, element selectors (CSS/XPath), or anti-bot mechanisms change. SeleniumBase's auto-waiting features mitigate flakiness but cannot prevent issues arising from fundamental site changes. Additionally, anti-bot services and CAPTCHAs (like reCAPTCHA) can still detect automation, especially after browser updates, even with stealth options.
- breaking As of recent versions (e.g., around v4.x), `allure-pytest` is no longer bundled with SeleniumBase and must be installed separately if you wish to generate Allure Reports.
- gotcha Overriding `setUp()` or `tearDown()` methods in a `BaseCase` subclass requires careful invocation of `super().setUp()` and `super().tearDown()` to ensure SeleniumBase's core setup and teardown functionalities (like browser initialization/quitting, screenshot capture) are not inadvertently bypassed or corrupted. Calling `super()` at the wrong point, or not at all, can lead to broken tests or resource leaks.
Install
-
pip install seleniumbase -
seleniumbase install chromedriver latest
Imports
- BaseCase
from seleniumbase import BaseCase
- SB
from seleniumbase import SB
- Driver
from seleniumbase import Driver
Quickstart
from seleniumbase import SB
with SB() as sb:
sb.open("https://seleniumbase.io/demo_page")
sb.type("#myTextInput", "This is an automated test")
sb.click("#myButton")
sb.assert_text("Automation Practice", "h3")
sb.highlight("#myButton")
sb.sleep(1)
sb.assert_element("tbody#tbodyId")
print("Test completed successfully!")