> For the complete documentation index, see [llms.txt](https://docs.pylenium.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pylenium.io/driver-commands/browser/execute_script.md).

# execute\_script

## Syntax

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

## Usage

{% code title="correct usage" %}

```python
# 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")
```

{% endcode %}

{% code title="incorrect usage" %}

```python
# Errors, 'execute_script' yields a WebElement, not a Pylenium Element
py.execute_script("return document.getElementById(arguments[0])").get()
```

{% endcode %}

## Arguments

* <mark style="color:purple;">`javascript (str)`</mark> - The javascript to execute
* <mark style="color:purple;">`*args (Any)`</mark> - A comma-separated list of arguments to pass into the javascript string

{% hint style="info" %}
You can access the **\*args** in the javascript by using `arguments[0]`, `arguments[1]`, etc.
{% endhint %}

## Yields

* <mark style="color:orange;">**Any**</mark> - This will return whatever is in the `return statement` of your javascript.

{% hint style="info" %}
If you do not include a **return**, then `.execute_script()` will return **None**
{% endhint %}

## Examples

```python
# 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
```

```python
# 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_script(get_siblings_script, "foo")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.pylenium.io/driver-commands/browser/execute_script.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
