Skip to main content

Easy explicit wait helpers for Selenium

Project description

PyPIVersion TravisCI CoverageStatus CodeHealth StoriesInReady PythonVersions Join the chat at https://gitter.im/levi-rs/explicit

Helper class to make working with Selenium explicit waits easier and more accessible

Explicit is designed to minimize or eleminate the common frustrations encountered when using Selenium on webpages with dynamicly loaded and/or javascript driven content. Typically, a developer will try to use the webdriver’s default find_element_by_<XPATH, CSS, ID, LINK TEXT, ETC> to locate an element, only to get hit with various exceptions like NoSuchElementException, StaleElementReferenceException, and so on.

Selenium includes several tools to address these limitations, most notibly implicit and explicit waits. While enabling the implicit wait is easy to do, it becomes increasingly problematic as scripts become more complex. Explicit waits offer a much more powerful alternative, giving the developer more fine tuned controls, but at the expense of added complexity.

The Explicit package abstracts away the complexities associated with explicit waits by wrapping commonly used functionality in an easy to use API.

Consider this example: You want to use Selenium to log into Github from the 404 page. You write a script like this to fill in the login credentials and click the login button:

from selenium import webdriver

driver = webdriver.Chrome()

try:
    driver.get("https://github.com/this/doesntexist")

    username_field = driver.find_element_by_id("login_field")
    username_field.click()
    username_field.send_keys("my_username")

    password_field = driver.find_element_by_id("password")
    password_field.click()
    password_field.send_keys("my_password")

    login_button = driver.find_element_by_css_selector("input.btn-primary")
    login_button.click()

finally:
    driver.quit()

When you run the program, however, you get an immediate exception:

(.venv35) ➜  explicit ✗ python example.py
Traceback (most recent call last):
File "example.py", line 9, in <module>
    username_field = driver.find_element_by_id("login_field")
<...>
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"login_field"}

The reason for the execption, which might not be readily apparent, is that the login modal on that page loads in after the page loads. When the script runs it attempts to immediately find the field element after control is returned from the driver.get call. Since the element isn’t in the DOM yet, Selenium throws the NoSuchElementException.

GithubLogin

Explicit easily solves this by waiting for the element to load in:

from explicit import waiter
from explicit import ID, CSS
from selenium import webdriver

driver = webdriver.Chrome()

try:
    driver.get("https://github.com/this/doesntexist")

    username_field = waiter.find_element(driver, "login_field", by=ID)
    username_field.click()
    username_field.send_keys("my_username")

    password_field = waiter.find_element(driver, "password", by=ID)
    password_field.click()
    password_field.send_keys("my_password")

    login_button = waiter.find_element(driver, "input.btn-primary", by=CSS)
    login_button.click()
finally:
    driver.quit()

Additionally, you can use explicit to handle the writing:

from explicit import waiter
from explicit import ID, CSS
from selenium import webdriver

driver = webdriver.Chrome()

try:
    driver.get("https://github.com/this/doesntexist")

    waiter.find_write(driver, "login_field", "my_username", by=ID)

    waiter.find_write(driver, "password", "my_password", by=ID, send_enter=True)

finally:
    driver.quit()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

explicit-0.1.3.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

explicit-0.1.3-py2.py3-none-any.whl (8.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file explicit-0.1.3.tar.gz.

File metadata

  • Download URL: explicit-0.1.3.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for explicit-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3e70c7365b73171467b11fce734fecad19f1a5cced4d17a85d90bc18d49545a5
MD5 b5a09d3b59809ca1c75e6ac3970820ec
BLAKE2b-256 124ed6eafe657717a08f245b5f7439cb91b81862ddeedeba9193f731a19d83a8

See more details on using hashes here.

File details

Details for the file explicit-0.1.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for explicit-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bb025f19bf5a2baf19b632275980224f1b24854dc0f2c69b9136ff2fe9f0262a
MD5 f2978ab24b91e4547f345c52bdf5e3dc
BLAKE2b-256 d147b586118021544b0e716fd14fe81444c4398212a9ed0f42819e1c78955c8d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page