github seleniumbase/SeleniumBase v4.6.3
4.6.3 - Flexible Driver Manager

latest releases: v4.31.4, v4.31.3, v4.31.2...
23 months ago

Flexible Driver Manager

  • Make the standalone Driver Manager more flexible.
    --> More options when using the Driver Manager without BaseCase.
    --> Added shorter method names as duplicates for some methods.
  • Refresh Python dependencies.
    --> e6e0971

The driver manager (via context manager)

This pure Python format gives you a raw webdriver instance in a with block. The SeleniumBase Driver Manager will automatically make sure that your driver is compatible with your browser version. It gives you full access to customize driver options via method args or via the command-line. The driver will automatically call quit() after the code leaves the with block. Here are some examples:

"""This script can be run with pure "python". (pytest not needed)."""
from seleniumbase import js_utils
from seleniumbase import page_actions
from seleniumbase import Driver

# Python Context Manager
with Driver() as driver:  # By default, browser="chrome"
    driver.get("https://google.com/ncr")
    js_utils.highlight_with_js(driver, 'img[alt="Google"]', 6, "")

with Driver() as driver:  # Also accepts command-line options
    driver.get("https://seleniumbase.github.io/demo_page")
    js_utils.highlight_with_js(driver, "h2", 5, "")
    by_css = "css selector"
    driver.find_element(by_css, "#myTextInput").send_keys("Automation")
    driver.find_element(by_css, "#checkBox1").click()
    js_utils.highlight_with_js(driver, "img", 5, "")

# Python Context Manager (with options given)
with Driver(browser="chrome", incognito=True) as driver:
    driver.get("https://seleniumbase.io/apps/calculator")
    page_actions.wait_for_element(driver, "4", "id").click()
    page_actions.wait_for_element(driver, "2", "id").click()
    page_actions.wait_for_text(driver, "42", "output", "id")
    js_utils.highlight_with_js(driver, "#output", 6, "")

(See examples/raw_driver.py for an example.)

The driver manager (via direct import)

Another way of running Selenium tests with pure python (as opposed to using pytest or nosetests) is by using this format, which bypasses BaseCase methods while still giving you a flexible driver with a manager. SeleniumBase includes helper files such as page_actions.py, which may help you get around some of the limitations of bypassing BaseCase. Here's an example:

"""This script can be run with pure "python". (pytest not needed)."""
from seleniumbase import Driver
from seleniumbase import js_utils
from seleniumbase import page_actions

# Example with options. (Also accepts command-line options.)
driver = Driver(browser="chrome", headless=False)
try:
    driver.get("https://seleniumbase.io/apps/calculator")
    page_actions.wait_for_element(driver, "4", "id").click()
    page_actions.wait_for_element(driver, "2", "id").click()
    page_actions.wait_for_text(driver, "42", "output", "id")
    js_utils.highlight_with_js(driver, "#output", 6, "")
finally:
    driver.quit()

# Example 2 using default args or command-line options
driver = Driver()
driver.get("https://seleniumbase.github.io/demo_page")
js_utils.highlight_with_js(driver, "h2", 5, "")
by_css = "css selector"
driver.find_element(by_css, "#myTextInput").send_keys("Automation")
driver.find_element(by_css, "#checkBox1").click()
js_utils.highlight_with_js(driver, "img", 5, "")
driver.quit()  # If the script fails early, the driver still quits

(From examples/raw_browser_launcher.py)

The above format can be used as a drop-in replacement for virtually every Python/selenium framework, as it uses the raw driver instance for handling commands. The Driver() method simplifies the work of managing drivers with optimal settings, and it can be configured via multiple method args. The Driver also accepts command-line options (such as python --headless) so that you don't need to modify your tests directly to use different settings. These command-line options only take effect if the associated method args remain unset (or set to None) for the specified options.

Don't miss a new SeleniumBase release

NewReleases is sending notifications on new releases.