SeleniumBase README

The project's README from github.com/seleniumbase/SeleniumBase. As one markdown file: README.md.

SeleniumBase

All-in-one Browser Automation Framework:
Web Crawling / Testing / Scraping / Stealth

seleniumbase%2FSeleniumBase | Trendshift

PyPI version SeleniumBase PyPI downloads SeleniumBase Docs SeleniumBase GitHub Actions YouTube Channel

🚀 Start | 🏰 Features | 🎛️ Options | 📚 Examples | 💻 Scripts | 🗾 Locale
📗 API | 📘 Stealth API | 🔠 DesignPatterns | 🔴 Recorder | 📊 Dashboard
🎖️ GUI | 📰 TestPage | 👤 UC Mode | 🐙 CDP Mode | 📶 Charts | 🖥️ Farm
👁️ How | 🚝 Migration | 🎭 Stealthy Playwright | 🛂 MasterQA | 🚎 Tours
🤖 MCP | 🟨 JS Mgr | 🌏 Translator | 🎞️ Presenter | 🖼️ Visual | 🗂️ CPlans



📝 Here's a Python example that uses Pure CDP Mode (sb_cdp):
(It navigates to Browserscan where it bypasses bot-detection.)

from seleniumbase import sb_cdp

sb = sb_cdp.Chrome()
sb.goto("https://browserscan.net/bot-detection")
sb.sleep(3)
sb.quit()

BrowserScan Test Results: Normal
(All BrowserScan bot-detection tests passed successfully.)

🎭 Here's an example script that uses Stealthy Playwright Mode:
(Playwright connects to a stealthy SeleniumBase browser session.)

from playwright.sync_api import sync_playwright
from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(guest=True)
endpoint_url = sb.get_endpoint_url()

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(endpoint_url)
    page = browser.contexts[0].pages[0]
    page.goto("https://bot.sannysoft.com/")
    page.wait_for_timeout(500)

All Sannysoft tests passed successfully
(All Sannysoft bot-detection tests passed successfully.)


📝 This example shows off multiple methods:

from seleniumbase import sb_cdp

sb = sb_cdp.Chrome()
sb.goto("https://seleniumbase.io/demo_page")
sb.type("input", "Quickly type text!")
sb.press_keys("textarea", "Slowly type text!")
sb.click("#myButton")
sb.set_value("input#mySlider", "100")
sb.click_visible_elements("input.checkBoxClassB")
sb.select_option_by_text("#mySelect", "Set to 75%")
sb.hover_and_click("#myDropdown", "#dropOption2")
sb.click("#checkBox1")
sb.drag_and_drop("img#logo", "div#drop2")
sb.nested_click("iframe#myFrame3", ".fBox")
sb.highlight("#myButton")
sb.quit()

📝 This example expands the Browserscan script into a test demo:
(Assertions added and elements highlighted with JavaScript.)

from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(locale="en", ad_block=True)
sb.goto("https://browserscan.net/bot-detection")
sb.flash("Test Results", duration=1.5, pause=0.5)
sb.assert_element('strong:contains("Normal")')
print("Bot Not Detected")
sb.flash('strong:contains("Normal")', pause=1)
sb.quit()

📝 This example scrapes Hacker News listings:

from seleniumbase import sb_cdp

sb = sb_cdp.Chrome()
sb.goto("https://news.ycombinator.com/submitted?id=seleniumbase")
elements = sb.find_elements("span.titleline > a")
for element in elements:
    print("* " + element.text)

🐙 Stealthy CDP Mode examples are located in ./examples/cdp_mode/.

🎭 Stealthy Playwright examples are located in ./examples/cdp_mode/playwright/.


⚙️ Stealthy architecture flowchart:

Stealthy architecture flowchart
(For maximum stealth, use CDP Mode, which includes Stealthy Playwright Mode.)


🌐 CLI Options for Supported Chromium Browsers

💡 You can set the Chromium browser to use via command line parameters:

python SCRIPT.py --chromium  # Use the unbranded Chromium browser
python SCRIPT.py --cft  # Use Chrome-for-testing
python SCRIPT.py --edge  # Use Microsoft Edge
python SCRIPT.py --brave  # Use Brave browser

Google Chrome is the default browser. Only unbranded Chromium and Chrome-for-Testing get downloaded automatically if not already present on the system.

The Chromium browser can also be set via method args, eg: cft=True, use_chromium=True, browser="edge", browser="brave", etc. Eg:

sb = sb_cdp.Chrome(use_chromium=True)

📝 This example saves Google Search results with UC + CDP Mode:
(Results are saved as PDF, HTML, and PNG files to ./latest_logs/)

from seleniumbase import SB

with SB(uc=True, test=True) as sb:
    url = "https://google.com/ncr"
    sb.activate_cdp_mode(url)
    sb.click_if_visible('button:contains("Accept all")')
    sb.type('[name="q"]', "SeleniumBase GitHub page")
    sb.click('[value="Google Search"]')
    sb.sleep(4)  # The "AI Overview" sometimes loads
    print(sb.get_page_title())
    sb.save_as_pdf_to_logs()
    sb.save_page_source_to_logs()
    sb.save_screenshot_to_logs()
    print("Logs have been saved to: ./latest_logs/")

📝 This example bypasses Cloudflare's challenge page with UC + CDP Mode:
(If the Turnstile isn't bypassed automatically, sb.solve_captcha() handles it.)

from seleniumbase import SB

with SB(uc=True, test=True, locale="en") as sb:
    url = "https://gitlab.com/users/sign_in"
    sb.activate_cdp_mode(url)
    sb.sleep(2)
    sb.solve_captcha()
    # (The rest is for testing and demo purposes)
    sb.assert_text("Username", '[for="user_login"]', timeout=3)
    sb.assert_element('label[for="user_login"]')
    sb.highlight('button:contains("Sign in")')
    sb.highlight('h1:contains("GitLab")')
    sb.post_message("SeleniumBase wasn't detected", duration=4)

SeleniumBase SeleniumBase
(Successfully bypassed bot-detection on a Cloudflare challenge page.)

💡 sb.solve_captcha() handles CAPTCHAs that aren't bypassed automatically.
(If no CAPTCHA is present on the current page, then nothing happens.)


📝 This example handles a CAPTCHA page with Pure CDP Mode:

from seleniumbase import sb_cdp

sb = sb_cdp.Chrome(incognito=True)
sb.goto("https://gitlab.com/users/sign_in")
sb.sleep(2)
sb.solve_captcha()
sb.highlight('h1:contains("GitLab")')
sb.highlight('button:contains("Sign in")')
sb.quit()

🧪 Comprehensive E2E Testing with pytest:

📚 The SeleniumBase/examples/ folder includes over 150 ready-to-run examples of E2E testing. Examples that start with test_ or end with _test.py/_tests.py run with pytest. Other examples run directly with raw python (those generally start with raw_ to avoid confusion).

📝 This example tests an e-commerce site with pytest:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)  # Call pytest

class MyTestClass(BaseCase):
    def test_swag_labs(self):
        self.goto("https://www.saucedemo.com")
        self.type("#user-name", "standard_user")
        self.type("#password", "secret_sauce\n")
        self.assert_element("div.inventory_list")
        self.click('button[name*="backpack"]')
        self.click("#shopping_cart_container a")
        self.assert_text("Backpack", "div.cart_item")
        self.click("button#checkout")
        self.type("input#first-name", "SeleniumBase")
        self.type("input#last-name", "Automation")
        self.type("input#postal-code", "77123")
        self.click("input#continue")
        self.click("button#finish")
        self.assert_text("Thank you for your order!")

pytest test_get_swag.py

SeleniumBase Test


📝 This example tests another e-commerce site with pytest:

pytest test_coffee_cart.py --demo

SeleniumBase Coffee Cart Test

(--demo mode slows down tests and highlights actions)


📝 This example covers multiple actions with pytest:

pytest test_demo_site.py

SeleniumBase Example

Easy to type, click, select, toggle, drag & drop, and more.

(For more examples, see the SeleniumBase/examples/ folder.)


SeleniumBase

Explore the README:


▶️ How is SeleniumBase different from raw Selenium? (click to expand)

💡 SeleniumBase is a Python framework for browser automation and testing. SeleniumBase uses Selenium/WebDriver APIs and incorporates test-runners such as pytest, pynose, and behave to provide organized structure, test discovery, test execution, test state (eg. passed, failed, or skipped), and command-line options for changing default settings (eg. browser selection). With raw Selenium, you would need to set up your own options-parser for configuring tests from the command-line.

💡 SeleniumBase's driver manager gives you more control over automatic driver downloads. (Use --driver-version=VER with your pytest run command to specify the version.) By default, SeleniumBase will download a driver version that matches your major browser version if not set.

💡 SeleniumBase automatically detects between CSS Selectors and XPath, which means you don't need to specify the type of selector in your commands (but optionally you could).

💡 SeleniumBase methods often perform multiple actions in a single method call. For example, self.type(selector, text) does the following:
1. Waits for the element to be visible.
2. Waits for the element to be interactive.
3. Clears the text field.
4. Types in the new text.
5. Presses Enter/Submit if the text ends in "\n".
With raw Selenium, those actions require multiple method calls.<

SeleniumBase (seleniumbase/SeleniumBase)

Searching Database…

这是干什么的?SeleniumBase 是一个一体化的 Python 浏览器自动化框架,集网页抓取、自动化测试、数据采集和隐身绕过检测于一身。它的 CDP Mode 能绕过基于 Chromium 内核浏览器中的机器人检测,而 Stealthy Playwright Mode 则把这种能力扩展到了 Playwright。
它是一个测试框架,还是一个爬虫库?两者都是,合在一个框架里:它封装了 Selenium/WebDriver 的 API,并能接入 pytest、pynose、behave 这类测试运行器,所以同一套调用既可以搭建规整的端到端(E2E)测试套件,也可以用来写爬虫和数据采集脚本。
怎么把它跑起来?只需 'pip install seleniumbase' 一条命令('uv add seleniumbase' 也行)。如果想用可选的 Stealthy Playwright Mode,还要再运行 'pip install playwright'。
它能驱动哪些浏览器?我还需要自己操心 chromedriver 的版本吗?支持基于 Chromium 内核的浏览器;默认使用 Google Chrome,如果没装,会自动下载无品牌版 Chromium 或 Chrome-for-Testing。驱动版本也帮你管好了——SeleniumBase 会自动匹配你的浏览器版本,除非你用 --driver-version=VER 手动指定。
绕过机器人检测在任何网站上都管用吗?不是每个网站都行——README 里演示了通过 BrowserScan 的全部检测以及 Cloudflare Turnstile 验证,但凡是没能自动绕过的情况,都会落到 sb.solvecaptcha() 上,而它只在页面上确实出现验证码时才会介入。
它主要是给测试工程师(QA)用的,还是也能用来爬数据?两类用户都适合:基于 pytest 的端到端(E2E)测试是它的核心,但同一套 API 也能驱动爬虫和数据采集——而且 CDP Mode 的机器人检测绕过,专门针对那些普通 Selenium 脚本搞不定的有防护网站。
我用的是 Windows、Mac 还是 Linux 有影响吗?每天的夜间 CI 都会在 macOS 和 Ubuntu 上跑完整测试套件,而且它是个 pip 包,只要有 Python 和一款 Chromium 内核浏览器就能运行。驱动哪个 Chromium 变体由你决定——方法参数里就有 browser='edge' 和 browser='brave'。
它和直接用原生 Selenium 有什么区别?原生 Selenium 只是裸的 WebDriver 调用。SeleniumBase 在此之上加了测试运行器集成(pytest、pynose、behave)、命令行选项、自动下载驱动、自动识别 CSS 还是 XPath 选择器,还有多步骤方法——比如 self.type() 会等元素可见且可交互,清空输入框,输入文字,如果内容以换行结尾还会按回车。
CDP Mode 实际都用来做什么?用于在有防护的网站上做隐身抓取和自动化:README 演示了通过 BrowserScan 的全部机器人检测、绕过 Cloudflare Turnstile 验证页,以及把 Google 搜索结果保存为 PDF、HTML 和 PNG 文件到 ./latestlogs/ 目录。Playwright 也能通过 Stealthy Playwright Mode 接入同一个隐身会话。
有没有 MCP 服务器,能让 AI 编程助手来驱动它?有——README 指向了一个 seleniumbase-mcp MCP 服务器,它是独立于主框架 'pip install seleniumbase' 的一个单独的包。
哪里能找到可以直接抄的隐身模式示例代码?Stealthy CDP Mode 的示例在 ./examples/cdpmode/ 目录,Stealthy Playwright 的示例在 ./examples/cdpmode/playwright/ 目录——其中包括通过 Sannysoft 和 BrowserScan 机器人检测的运行示例。
Size
15.1 MB
Stars
13.1K
Age
12y
Commits · 90 Days