Skip to content

browser.combo#

Functions#

log_in(login_credentials, login_form, timeout=None) #

Standardised combination of methods to log in.

Note

Most websites process login in either one or two steps. Use LoginForm1Step when username and password are prompted on the same page. Use LoginForm2Steps when username is prompted first, and then the option to input password appears later on the same or a separate page. The two-step variation is often to verify whether a user exists or not before password can be entered (or should be redirected to a registration page).

Parameters:

Name Type Description Default
login_credentials LoginCredentials

Apply username and password here.

required
login_form LoginForm1Step | LoginForm2Steps

Add settings class.

required
timeout float | None

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

None

Returns:

Type Description
bool | None

If return_bool is True in the settings class, the login combo can be used with a conditional if statement and return boolean True or False depending on whether the login form was handled succesfully without errors or not. This will also suppresses exceptions. With default False, the login combo will not return any value. Note that either of the parameters post_login_url_contains or post_login_element_xpath or both also need to be defined in the settings class.

Example
from browserist import Browser, LoginForm1Step, LoginCredentials

login_credentials = LoginCredentials(
    username="some_username",
    password="some_password")

login_form = LoginForm1Step(
    url="https://example.com/login",
    username_input_xpath="//xpath/to/username_field",
    password_input_xpath="//xpath/to/password_field",
    submit_button_xpath="//xpath/to/login_button")

with Browser() as browser:
    browser.combo.log_in(login_credentials, login_form)
    browser.open.url("https://example.com/some_page")
    browser.click.button("//xpath/to/button")

Or use succesfull handling of the login with a conditional if statement by setting return_bool to True as parameter in the settings class:

from browserist import Browser, LoginForm1Step, LoginCredentials

login_credentials = LoginCredentials(
    username="some_username",
    password="some_password")

login_form = LoginForm1Step(
    url="https://example.com/login",
    username_input_xpath="//xpath/to/username_field",
    password_input_xpath="//xpath/to/password_field",
    submit_button_xpath="//xpath/to/login_button",
    post_login_url_contains="https://example.com/successfull_logged_in_page",
    post_login_element_xpath="//xpath/to/successfull_logged_in_element",
    return_bool=True)

with Browser() as browser:
    if browser.combo.log_in(login_credentials, login_form):
        browser.open.url("https://example.com/some_page")
        browser.click.button("//xpath/to/button")

Configuration Classes#

LoginCredentials#

Settings class for the login credentials of a user profile.

Note

Separated from the login form so that the multiple users/roles can use the same form.

Parameters:

Name Type Description Default
username str

Username, e.g. email.

required
password str

Password.

required

LoginForm1Step#

Settings for login form page in 1 step where both username and password are displayed at once.

Parameters:

Name Type Description Default
url str | None

URL to login page.

None
username_input_xpath str

XPath for username input field.

required
password_input_xpath str

XPath for password input field.

required
submit_button_xpath str

XPath for submit button.

required
post_login_wait_seconds float | None

Minor grace time to ensure the login has completed.

None
post_login_url_contains str | None

Optionally await redirect to this URL as a user is typically redirected automatically to a new page or view after logging in.

None
post_login_element_xpath str | None

Upon successful login, optionally await this element to be loaded.

None
return_bool bool

If set to True, the login combo can be used with a conditional if statement and return boolean True or False depending on whether the login form was handled succesfully without errors or not. This will also suppresses exceptions. With default False, the login combo will not return any value. Note that either of the parameters post_login_url_contains or post_login_element_xpath or both also need to be defined in the settings class.

False

LoginForm2Steps#

Settings for login form page in 2 steps where username is prompted first, and once confirmed, then the password can be entered.

Parameters:

Name Type Description Default
url str | None

URL to login page.

None
username_input_xpath str

XPath for username input field.

required
username_submit_button_xpath str

XPath for username submit button.

required
password_input_xpath str

XPath for password input field.

required
password_submit_button_xpath str

XPath for password submit button.

required
post_login_wait_seconds float | None

Minor grace time to ensure the login has completed.

None
post_login_url_contains str | None

Optionally await redirect to this URL as a user is typically redirected automatically to a new page or view after logging in.

None
post_login_element_xpath str | None

Upon successful login, optionally await this element to be loaded.

None
return_bool bool

If set to True, the login combo can be used with a conditional if statement and return boolean True or False depending on whether the login form was handled succesfully without errors or not. This will also suppresses exceptions. With default False, the login combo will not return any value. Note that either of the parameters post_login_url_contains or post_login_element_xpath or both also need to be defined in the settings class.

False