Skip to content

browser#

Main class of Browserist that sets the Selenium web driver and contains all helper functions.

Initiates the browser driver whether the settings call for Chrome, Edge, Firefox, etc.

Example
1
2
3
4
5
6
from browserist import Browser, BrowserSettings

settings = BrowserSettings(browser_type=BrowserType.FIREFOX)

with Browser(settings) as browser:
    browser.open.url("https://example.com")

Functions#

back() #

Press the browser's back button.

Example
1
2
3
4
5
6
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    browser.open.url("https://google.com")
    browser.back()  # Go back to previous page Example.com

forward() #

Press the browser's forward button.

Example
1
2
3
4
5
6
7
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    browser.open.url("https://google.com")
    browser.back()  # Go back to previous page Example.com
    browser.forward()  # Return to Google.com

quit() #

Quit the browser.

Example
1
2
3
4
5
from browserist import Browser

browser = Browser()
browser.open.url("https://example.com")
browser.quit()
Tip

Instead of manually quitting the browser with browser.quit(), it's recommend to use the context manager and with statements. The example above could then be refactored to:

Python
1
2
3
4
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")

refresh() #

Refresh the current page.

Example
browser.refresh()

Configuration Classes#

BrowserSettings#

Class to configure the browser driver.

Parameters:

Name Type Description Default
type BrowserType

Set browser type, e.g. Chrome, Edge, Firefox, etc.

EDGE if is_windows() else CHROME
headless bool

Run the browser in headless mode. May not be supported by all browsers, or some interaction methods, e.g. select, may not be supported.

False
disable_images bool

Neither request nor render images, which typically improves loading speed. May not be supported by all browsers.

False
page_load_strategy PageLoadStrategy NORMAL
path_to_executable str | Path | None

If the browser executable isn't in a default folder, select which file to use.

None
download_dir str | Path

Set where to save downloads. Default is the Downloads folder of the user.

DOWNLOADS_DIR
screenshot_dir str | Path

Set where to save sreenshots. Default is the Downloads folder of the user.

DOWNLOADS_DIR
timeout TimeoutSettings TimeoutSettings()
viewport DeviceViewportSize | tuple[int, int] | None

Emulate viewport size as device or set custom value in pixels. If not set, the browser's default size is used.

None
check_connection bool

Check that there is an internet connection before starting the browser. Bypass the check by setting it to False.

True
Example

Use Firefox as browser type:

1
2
3
4
5
6
from browserist import Browser, BrowserSettings

settings = BrowserSettings(browser_type=BrowserType.FIREFOX)

with Browser(settings) as browser:
    browser.open.url("https://example.com")

Use browser in headless mode and with images disabled:

1
2
3
4
5
6
from browserist import Browser, BrowserSettings

settings = BrowserSettings(headless=True, disable_images=True)

with Browser(settings) as browser:
    browser.open.url("https://example.com")

Use custom directory for screenshots:

1
2
3
4
5
6
7
from browserist import Browser, BrowserSettings

settings = BrowserSettings(screenshot_dir="/path/to/screenshots")

with Browser(settings) as browser:
    browser.open.url("https://example.com")
    browser.screenshot.visible_portion()

Use custom viewport size:

1
2
3
4
5
6
from browserist import Browser, BrowserSettings

settings = BrowserSettings(viewport=(1024, 768))

with Browser(settings) as browser:
    browser.open.url("https://example.com")