> For the complete documentation index, see [llms.txt](https://mucsi96.gitbook.io/w3c-webdriver/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mucsi96.gitbook.io/w3c-webdriver/document.md).

# Document

## session.getPageSource()

Returns a string serialization of the DOM of the current browsing context active document.

**RETURNS**

Promise\<string>

**EXAMPLES**

```typescript
const source = await session.getPageSource();
// source = '<!DOCTYPE html><head><title>...'
```

**SEE ALSO**

* [WebDriver spec](https://www.w3.org/TR/webdriver/#get-page-source)

## session.executeScript\<T>(script, args)

Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be synchronous and the result of evaluating the script is returned to the client.

**PARAMETERS**

* `script`: string - JavaScript to execute in browser context
* `args`: unknown\[] - Arguments to sent to executed script

**RETURNS**

Promise\<T>

**EXAMPLES**

```typescript
const script = `
  const [name] = arguments;
  return `Hello from ${name}!`;
`;
const message = await session.executeScript(script, ['WebDriver']);
// message = 'Hello from WebDriver!'
```

```typescript
const button = await session.findElement('css selector', '#red-button');
const script = `
   const [element] = arguments;
   return element.id;
`;
const id = await session.executeScript(script, [button]);
// id = 'red-button'
```

```typescript
const script = `
   return document.querySelector('#red-button');
`;
const button = await session.executeScript(script);
const id = await button.getProperty('id');
// id = 'red-button'
```

**SEE ALSO**

* [WebDriver spec](https://www.w3.org/TR/webdriver/#execute-script)

## session.executeAsyncScript\<T>(script, args)

causes JavaScript to execute as an anonymous function. Unlike the Execute Script command, the result of the function is ignored. Instead an additional argument is provided as the final argument to the function. This is a function that, when called, returns its first argument as the response.

**PARAMETERS**

* `script`: string - JavaScript to execute in browser context
* `args`: unknown\[] - Arguments to sent to executed script

**RETURNS**

Promise\<T>

**EXAMPLES**

```typescript
const script = `
  const [a, b, callback] = arguments;
  setTimeout(() => callback(a * b), 1000);
`;
const message = await session.executeAsyncScript(script, [5, 3]);
// message = 15
```

```typescript
const button = await session.findElement('css selector', '#red-button');
const script = `
   const [element, callback] = arguments;
   callback(element.id);
`;
const id = await session.executeAsyncScript(script, [button]);
// id = 'red-button'
```

```typescript
const script = `
   const [callback] = arguments;
   callback(document.querySelector('#red-button'));
`;
const button = await session.executeAsyncScript(script);
const id = await button.getProperty('id');
// id = 'red-button'
```

**SEE ALSO**

* [WebDriver spec](https://www.w3.org/TR/webdriver/#execute-async-script)


---

# 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:

```
GET https://mucsi96.gitbook.io/w3c-webdriver/document.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
