Pylenium.io
GitHub ↗️
  • Welcome to Pylenium
  • Getting Started
    • 1. Virtual Environments
    • 2. Setup pytest
    • 3. Project Structure with pytest
    • 4. Writing Tests with Pylenium
  • Guides
    • πŸ“ŠVisualize Test Results with Allure
    • πŸ“Logging
    • 🟨Run Tests in Containers
    • πŸ”€Run Tests in Parallel
    • πŸ§ͺShould / Expected Conditions
    • 🌐Script with Standalone Pylenium
  • CLI
    • πŸ’»Pylenium CLI
    • πŸ“ŠAllure CLI
  • Configuration
    • πŸ“„pylenium.json
    • πŸš—Driver
    • πŸ–₯️Viewport
  • Fixtures
    • ↗️api
    • πŸͺ“axe
    • πŸ₯Έfake
    • βœ…py
    • β˜‘οΈpyc
    • ❌pys
  • Driver Commands
    • Overview
    • πŸ—ΊοΈNavigation
      • go
      • quit
      • reload
      • visit
    • πŸ”Find Elements
      • contains
      • find
      • findx
      • get
      • getx
    • ⏱️Driver.should()
    • 🌐Browser
      • execute_script
      • execute_async_script
      • maximize_window
      • screenshot
      • scroll_to
      • title
      • url
      • window_handles
      • window_size
      • viewport
    • πŸͺCookies
      • delete_all_cookies
      • delete_cookie
      • get_all_cookies
      • get_cookie
      • set_cookie
    • πŸ”„Switch To
      • default_content
      • frame
      • frame_by_element
      • parent_frame
      • window
      • new_window
      • new_tab
    • πŸ“ŠWeb Performance
      • Performance API
      • CDP Performance
    • fake
    • wait
    • webdriver
  • Element Commands
    • πŸ”Find Elements
    • ⏱️Element.should()
    • πŸ‘Actions
      • check
      • clear
      • click
      • deselect
      • double_click
      • drag_to
      • drag_to_element
      • focus
      • hover
      • right_click
      • scroll_into_view
      • select_by_index
      • select_by_text
      • select_by_value
      • submit
      • type
      • uncheck
      • upload
    • πŸ”’Element Data
      • css_value
      • get_attribute
      • get_property
      • tag_name
      • text
      • is_checked
      • is_displayed
      • is_enabled
      • is_selected
    • πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦Family
      • children
      • parent
      • siblings
    • open_shadow_dom
    • screenshot
    • webelement
  • Elements Commands
    • ⏱️Elements.should()
    • first
    • length
    • last
    • is_empty
  • Contribute
    • Clone and Setup the Project
Powered by GitBook
On this page
  • Syntax
  • Usage
  • Arguments
  • Yields
  • Examples
  1. Driver Commands
  2. Browser

execute_async_script

The command to execute async javascript into the browser.

Previousexecute_scriptNextmaximize_window

Last updated 3 years ago

Similar to the command, you can pass in any javascript string and *args. The main difference is that you can execute asynchronous javascript. For example, using callbacks.

script = "var callback = arguments[arguments.length - 1]; " \
         "window.setTimeout(function(){ callback('timeout') }, 3000);"
driver.execute_async_script(script)

Syntax

py.execute_async_script(javascript: str) -> Any
py.execute_async_script(javascript: str, *args) -> Any

Usage

correct usage
# Yields the value of document.title
py.execute_async_script("return document.title;")

---or---

# Yields the .innerText of the element with the id of 'foo'
py.execute_async_script("return document.getElementById(arguments[0]).innerText", "foo")
incorrect usage
# Errors, 'execute_script' yields a WebElement, not a Pylenium Element
py.execute_async_script("return document.getElementById(arguments[0])").get()

Arguments

  • javascript (str) - The async javascript to execute

  • *args (Any) - A comma-separated list of arguments to pass into the javascript string

You can access the *args in the javascript by using arguments[0], arguments[1], etc.

Yields

  • Any - This will return whatever is in the return statement of your javascript.

If you do not include a return, then .execute_async_script() will return None

Examples

# You can pass in complex objects
ul_element = py.get("ul")
py.execute_script("return arguments[0].children;", ul_element.webelement)
# We use the .webelement property to send Selenium's WebElement
# that is understood by the browser
# You can create complex javascript strings
get_siblings_script = '''
    elem = document.getElementById(arguments[0]);
    var siblings = [];
    var sibling = elem.parentNode.firstChild;

    while (sibling) {
        if (sibling.nodeType === 1 && sibling !== elem) {
            siblings.push(sibling);
        }
        sibling = sibling.nextSibling
    }
    return siblings;
    '''
siblings = self.py.execute_async_script(get_siblings_script, "foo")
🌐
execute_script