execute_script

The command to execute javascript into the browser.

Syntax

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

Usage

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

---or---

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

Arguments

  • javascript (str) - The 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_script() will return None

Examples

Last updated