告别Selenium痛点!全新升级版Selenium自动化框架

B站影视 内地电影 2025-05-26 12:23 3

摘要:在使用 Selenium 编写 Web 自动化脚本时,元素加载时机问题总是让人抓狂:要么脚本跑得太快找不到元素,要么得手动加一堆 time.sleep ,测试还经常因为浏览器驱动问题翻车。

在使用 Selenium 编写 Web 自动化脚本时,元素加载时机问题总是让人抓狂:要么脚本跑得太快找不到元素,要么得手动加一堆 time.sleep ,测试还经常因为浏览器驱动问题翻车。

好不容易跑通测试,却被 Cloudflare 检测拦截,测试结果还得手动整理,效率低到让人怀疑人生。

作为一名常年与 Web 测试和爬虫打交道的开发者,我深知这些痛点。

不过最近我发现了一款超级给力的开源 Python 框架:SeleniumBase,它深度封装了 Selenium,内置智能等待、自动驱动管理和隐身模式,彻底解决了这些问题,让 Web 自动化变得简单、可靠、甚至有点“爽”!

在 GitHub 上已收获 10.2k+ 星标。它不仅适合自动化测试,还能轻松应对爬虫、网页操作等场景,提供直观的测试报告和实时仪表板,堪称 Web 开发者的“效率利器”。

核心功能

智能等待机制:自动检测元素是否加载完毕,无需手动添加 time.sleep 或 WebDriverWait。多种编写方式:支持 BaseCase 类继承和 SB 上下文管理器,满足不同开发习惯。UC Mode 隐身模式:自动规避 Cloudflare、人机验证等反爬机制。自动驱动管理:无需手动下载 Chrome/Firefox 驱动,SeleniumBase 自动匹配浏览器版本。内置测试报告与仪表板:提供详细的 HTML 测试报告和实时仪表板,展示测试通过率、失败详情和截图。丰富断言和操作:提供许多断言方法、页面操作,代码更简洁,断言更直观。快速入手

SeleniumBase 的安装和使用也极其简单,由于它是一个 Python 三方框架库,可以直接通过 pip 命令进行安装。

pip install seleniumbase

当然也能够通过下载项目源代码方式来。

git clone https://github.com/seleniumbase/SeleniumBase.gitcd SeleniumBase/pip install -e .

通过输入 seleniumbase 或 sbase 以验证 SeleniumBase 是否成功安装:

代码使用示例1:执行Google搜索

from seleniumbase import SBwith SB(test=True, uc=True) as sb:sb.open("https://google.com/ncr")sb.type('[title="Search"]', "SeleniumBase GitHub page\n")sb.click('[href*="github.com/seleniumbase/"]')sb.save_screenshot_to_logs # ./latest_logs/print(sb.get_page_title)

代码使用示例2:绕过Cloudflare验证页面

from seleniumbase import SBwith SB(uc=True, test=True, locale="en") as sb:url = "https://gitlab.com/users/sign_in"sb.activate_cdp_mode(url)sb.uc_gui_click_captchasb.sleep(2)

代码使用示例3:登录一个电子商务网站

from seleniumbase import BaseCaseBaseCase.main(__name__, __file__) # Call pytestclass MyTestClass(BaseCase):def test_swag_labs(self):self.open("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!")

代码使用示例4:轻松输入、点击、选择、切换、拖放等。

from seleniumbase import BaseCaseBaseCase.main(__name__, __file__)class DemoSiteTests(BaseCase):def test_demo_site(self):# Open a web page in the active browser windowself.open("https://seleniumbase.io/demo_page")# Assert the title of the current web pageself.assert_title("Web Testing Page")# Hover & click a dropdown element and assert resultsself.assert_text("Automation Practice", "h3")try:self.hover_and_click("#myDropdown", "#dropOption2", timeout=1)except Exception:# Someone probably moved the mouse while the test ranself.hover_and_js_click("#myDropdown", "#dropOption2")self.assert_text("Link Two Selected", "h3")# Assert exact textself.assert_exact_text("Demo Page", "h1")# Highlight a page element (Also asserts visibility)self.highlight("h2")# Actions with Demo Mode enabledif self.headed:self.activate_demo_modeself.type("input", "Have a Nice Day!")self.assert_text("SeleniumBase", "h2")

整个上手过程约 10-30 分钟,零基础也能快速写出稳定脚本!还有更多关于seleniumbase api的用法,可以参照官方文档操作。

官方文档:https://seleniumbase.io

应用场景

SeleniumBase 的灵活性和强大功能让它适用于多种场景,直击 Web 自动化的痛点:

端到端测试:自动化测试 Web 应用(如登录、表单提交),智能等待确保稳定性,报告美观专业。数据爬取:UC Mode 绕过反爬检测,轻松抓取电商、新闻网站数据,适合市场分析。自动化工作流:自动填写表单、批量下载文件,提升企业效率(如 HR 系统操作)。教学与学习:初学者用示例代码学习自动化测试,QA 工程师快速上手端到端测试。CI/CD 集成:与 Jenkins、GitHub Actions 集成,运行回归测试,生成仪表板。

这些场景都解决了一个核心痛点:Selenium 原生操作复杂、脚本不稳定、报告繁琐。SeleniumBase 像一个“智能助手”,让自动化测试和任务变得简单可靠。

SeleniumBase是一个基于 Selenium 的高层封装 Python 框架,解决 Web 自动化脚本中 80% 的痛点问题,让你写脚本像写自然语言一样流畅。

让你用几行代码就能写出稳定、可靠的 Web 自动化脚本,还能自动生成炫酷报告,让你的开发效率直接起飞!

就像给 Web 自动化装上了“智能引擎”。通过智能等待、UC Mode、自动驱动管理和炫酷报告,明显地解决了 Selenium 的短板,几行代码就能实现复杂操作。

适合新手入门、老手提效、企业做 UI 测试、开发者做数据控制,把原本易崩溃的 Selenium 脚本体验,变成写起来像玩一样的自动化流程。

GitHub 项目地址:https://github.com/seleniumbase/SeleniumBase

来源:微迅科技

相关推荐