Skip to content

browser.tool#

Functions#

count_elements(xpath, timeout=None) #

Count number of elements.

Parameters:

Name Type Description Default
xpath str

XPath of the elements.

required
timeout float | None

In seconds. Timeout to wait for element. If None, the global timeout setting is used (default 5 seconds).

None

Returns:

Type Description
int

Number of elements.

Example
from browserist import Browser

with Browser() as browser:
    url = "https://example.com"
    browser.open.url(url)
    link_count = browser.tool.count_elements("//a")
    if link_count > 0:
        print(f"Found {link_count} link(s) on {url}")
    else:
        print(f"No links found on {url}")

execute_script(script, element=None) #

Execute JavaScript, either with WebElement or without.

Parameters:

Name Type Description Default
script str

JavaScript code.

required
element WebElement | None

If given, execute JavaScript with WebElement.

None

Returns:

Type Description
Any

Return value given by the JavaScript code.

Example

Without WebElement:

browser.tool.execute_script("alert('Hello world!')")

With WebElement:

element = browser.get.element("//xpath/to/element")
browser.tool.execute_script("arguments[0].scrollIntoView();", element)

For example, how to scroll to the first link on a page:

1
2
3
4
5
6
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    first_link_element = browser.get.element("//a[1]")
    browser.tool.execute_script("arguments[0].scrollIntoView();", first_link_element)

is_input_valid(text, regex, ignore_case=True) #

Check if text input matches regex condition.

Parameters:

Name Type Description Default
text str

Input text.

required
regex str

Condition as regular expression.

required
ignore_case bool

Ignore case when comparing input text to condition.

True

Returns:

Type Description
bool

True if input matches condition, False otherwise.

Example

How to prompt the user for input in the terminal and hereafter validate the value before posting the form input:

1
2
3
4
5
6
7
8
9
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    user_input = input("Input value:")
    while not browser.tool.is_input_valid(user_input, r"regex"):
        print("Invalid input. Please try again...")
        user_input = input("Input value:")
    browser.input.value("//xpath/to/input", user_input)

is_url_valid(url) #

Check if input is a valid URL.

Parameters:

Name Type Description Default
url str

Input URL.

required

Returns:

Type Description
bool

True if input is a valid URL, False otherwise.

Example

How to prompt the user for a valid URL in the terminal:

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

with Browser() as browser:
    user_url = input("Input URL:")
    while not browser.tool.is_url_valid(user_url)
        print("Invalid URL. Please try again...")
        user_url = input("Input URL:")
    browser.open.url(user_url)

is_xpath_valid(xpath) #

Check if input is a valid XPath expression.

Parameters:

Name Type Description Default
xpath str

Input XPath.

required

Returns:

Type Description
bool

True if input is a valid XPath expression, False otherwise.

Example

How to prompt the user for a valid XPath value in the terminal:

1
2
3
4
5
6
7
8
9
from browserist import Browser

with Browser() as browser:
    browser.open.url("https://example.com")
    user_xpath = input("Input XPath:")
    while not browser.tool.is_xpath_valid(user_xpath)
        print("Invalid XPath. Please try again...")
        user_xpath = input("Input XPath:")
    browser.click.button(user_xpath)