Utilities Module sw.utils

This module primarily serves as a holder for all directly webdriver-facing functions. These functions usually serve only the purpose of interfacing with elements on the page and possibly logging when an error occurs. A lot of the code of these functions revolves around waiting.

Common Parameters

These parameters apply for functions in this module which link back to this documentation. In particular: sleepwait(), waitToDisappear(), and exists().

Common Params:
  • driver – The webdriver instance that is being acted on by this script.
  • element – An identifier for the element to search for.
  • type – The type of element‘s identifier.
Common Kwargs:
  • timeout (15) – The amount of time, in seconds, before continuing on if the element is not found. Note that in some functions, such as sleepwait(), this can be fatal to the script. A timeout in sleepwait will cause the script to either kill the child process (if die is enabled) or return None which webdriver cannot interface with.
  • lightConfirm (False) – Only checks if an element exists, does not verify if it is enabled or visible. This saves some CPU usage but can be dangerous in the case of a disabled web prompt.
  • cache (True) – Determines if elements will be cached internally within our sw.child process. This uses RAM over the a single run of the script, but has been shown to have speed savings in situations with a great deal of waiting. See sw.cache for more information.
  • url (driver.current_url) – Current URL of the page webdriver is on. If this is not specified it is pulled from our webdriver every time, which can get expensive over the lifetime of a script with a lot of waiting. This is only used if cache is enabled and is otherwise ignored.
  • thinkTime (child.sleepTime) – The time the script waits between polling for an element’s existance. This is usually the child’s sleep time, but for waitToDisappear() it is twice that.
  • die (True) – Whether or not the function kills the child if this is not found. Even if False, in the case of sleepwait() at least, there will usually be a fatal error anyway, especially if a WebElement is expected to be returned. sleepwait() typically returns a WebElement but if the element is not found (and die is False) returns None.

Functions

sw.utils.exists(driver, element, type, **kwargs)[source]

Checks if an element exists with the WebDriver functions. Catches and handles exceptions if it doesn’t. Previously there was an issue where the find_element_by... would wait for 15 seconds to find the element, but that has been resolved. If an element is in the DOM, does a final check to see if it is displayed and available.

Parameters:
Kwargs:
Returns:

Boolean if doesn’t exist, WebElement if it does.

sw.utils.isDisplayed(e)[source]

Does a check to see if the element is displayed while catching exceptions safely. Often when the script needs to see if an element is displayed, it isn’t even on the page. This can kill the program.

Parameters:e – An active WebElement that will be checked if it is displayed.
Returns:Boolean where True if the element is displayed and False if it is not.
sw.utils.isEnabled(e)[source]

Does a check to see if an element is enabled while capturing exceptions safely. Often when the script needs to see if an element is enabled, it isn’t. This normally kills the script and is undesireable.

Parameters:e – An active WebElement that will be checked if it is enabled (can type into / click).
Returns:Boolean where True if the element is enabled and False if it is not.
sw.utils.loadScript(driver, js)[source]

Creates a new script object and appends it to the header.

Parameters:
  • driver
  • js – URL to a JavaScript file that will be inserted and loaded on this page.
Returns:

None

sw.utils.sendKeys(driver, element, type, text)[source]

Drop in, faster replacement for send_keys(). Currently only supports fields with an id or name identifier.

Parameters:
Returns:

None

sw.utils.sleepwait(driver, element, type, **kwargs)[source]

The original brainchild of this wrapper, this function simply checks if an element exists( ) and sleeps until timeout for it. It always returns something, even if it fails.

Parameters:
Kwargs:
Returns:

Boolean if doesn’t exist, WebElement if it does.

sw.utils.waitToDisappear(driver, element, **kwargs)[source]

Waits for an element to disappear from the page. Useful for transparent overlays that appear routinely as those block all input on the page (which angers WebDriver). Optionally can wait for an element to reappear and call itself again to wait longer.

Parameters:
Kwargs:
  • type (“id”)
  • die – This is only passed to subfunctions. waitToDisappear() never ends the script if something does not disappear.
(wait-related):
  • timeout (60)
  • thinkTime (2)
  • stayGone (0) – Amount of time in seconds we wait, checking that the element is really gone.
  • waitForElement (True) – If the element doesn’t initially exist on the page, this controls if waitToDisappear waits for it first.
  • waitTimeout (3) – Number of seconds we wait for the element to appear. If the element doesn’t exist after this timeout, the function returns.
(internal):
  • cache
  • offset (0) – Used internally so timeout still applies to recursive calls. This offsets the next timeout by the amount of time waited in the previous call.
  • recur (False) – Internally used to not print to the log if this function calls itself again.
Returns:

None

Table Of Contents

Previous topic

ElementCache Module sw.cache

Next topic

Formatting Module sw.formatting

This Page