execute_async_script
The command to execute async javascript into the browser.
Similar to the execute_script 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
# 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")
# 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
Yields
Any - This will return whatever is in the
return statement
of your javascript.
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")
Last updated