SeleniumBase
SeleniumBase 是专业用于网络自动化活动的工具包。专为测试网站、绕过验证码、提高生产力、完成任务和扩展业务而构建
介绍
SeleniumBase 是一个用于浏览器自动化和测试的 Python 框架。SeleniumBase 使用 Selenium/WebDriver API,并集成了 pytest
、 pynose
和 behave
等测试运行器,以提供有组织的结构、测试发现、测试执行、测试状态(例如,通过、失败或跳过)以及用于更改默认设置的命令行选项(例如,浏览器选择)。使用原始 Selenium,您需要自己设置选项解析器来从命令行配置测试
SeleniumBase 允许您更改方法的显式超时值:
self.click("button", timeout=10)
使用原始 Selenium,这需要更多的代码:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable("css selector", "button")).click()
安装
安装 seleniumbase
:
pip install seleniumbase
从 GitHub 克隆安装 seleniumbase
:
git clone https://github.com/seleniumbase/SeleniumBase.git
cd SeleniumBase/
pip install -e .
升级从 GitHub 克隆的现有安装:
git pull
pip install -e .
方法
self.open(url) # Navigate the browser window to the URL.
self.type(selector, text) # Update the field with the text.
self.click(selector) # Click the element with the selector.
self.click_link(link_text) # Click the link containing text.
self.go_back() # Navigate back to the previous URL.
self.select_option_by_text(dropdown_selector, option)
self.hover_and_click(hover_selector, click_selector)
self.drag_and_drop(drag_selector, drop_selector)
self.get_text(selector) # Get the text from the element.
self.get_current_url() # Get the URL of the current page.
self.get_page_source() # Get the HTML of the current page.
self.get_attribute(selector, attribute) # Get element attribute.
self.get_title() # Get the title of the current page.
self.switch_to_frame(frame) # Switch into the iframe container.
self.switch_to_default_content() # Leave the iframe container.
self.open_new_window() # Open a new window in the same browser.
self.switch_to_window(window) # Switch to the browser window.
self.switch_to_default_window() # Switch to the original window.
self.get_new_driver(OPTIONS) # Open a new driver with OPTIONS.
self.switch_to_driver(driver) # Switch to the browser driver.
self.switch_to_default_driver() # Switch to the original driver.
self.wait_for_element(selector) # Wait until element is visible.
self.is_element_visible(selector) # Return element visibility.
self.is_text_visible(text, selector) # Return text visibility.
self.sleep(seconds) # Do nothing for the given amount of time.
self.save_screenshot(name) # Save a screenshot in .png format.
self.assert_element(selector) # Verify the element is visible.
self.assert_text(text, selector) # Verify text in the element.
self.assert_exact_text(text, selector) # Verify text is exact.
self.assert_title(title) # Verify the title of the web page.
self.assert_downloaded_file(file) # Verify file was downloaded.
self.assert_no_404_errors() # Verify there are no broken links.
self.assert_no_js_errors() # Verify there are no JS errors.
例子
执行谷歌搜索
from seleniumbase import SB
with SB(test=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())
绕过 Cloudflare 挑战页面的一个示例
from seleniumbase import SB
with SB(uc=True, test=True, locale_code="en") as sb:
url = "https://gitlab.com/users/sign_in"
sb.activate_cdp_mode(url)
sb.uc_gui_click_captcha()
sb.sleep(2)