# Interacting with Browser Tasks

Once you've initiated a browser task in **Browser Swarm**, you can connect to it using your preferred automation framework to perform various actions such as navigation, form submissions, and data extraction.

***

### 🔗 Connecting to a Browser Task

After creating a browser task, you'll receive a `connectUrl` which is used to establish a connection with the browser instance. Here's how to connect using different frameworks:

#### 📘 Playwright

{% tabs %}
{% tab title="Node.js" %}

```javascript
import { chromium } from 'playwright-core';
import { createSession } from 'browser-swarm-sdk';

const session = await createSession({ browser: 'chromium' });
const browser = await chromium.connectOverCDP(session.connectUrl);
const page = await browser.newPage();

await page.goto('https://example.com');
```

{% endtab %}

{% tab title="Python" %}

```python
from playwright.sync_api import sync_playwright
from browser_swarm import create_session

session = create_session(browser='chromium')

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp(session.connect_url)
    page = browser.new_page()
    page.goto('https://example.com')
```

{% endtab %}
{% endtabs %}

#### 🧪 Puppeteer

{% tabs %}
{% tab title="Node.js" %}

```javascript
import puppeteer from 'puppeteer-core';
import { createSession } from 'browser-swarm-sdk';

const session = await createSession({ browser: 'chromium' });
const browser = await puppeteer.connect({ browserWSEndpoint: session.connectUrl });
const page = await browser.newPage();

await page.goto('https://example.com');
```

{% endtab %}
{% endtabs %}

#### 🧪 Selenium

{% tabs %}
{% tab title="Python" %}

```python
from selenium import webdriver
from browser_swarm import create_session

session = create_session(browser='chrome')
options = webdriver.ChromeOptions()
options.add_experimental_option('debuggerAddress', session.connect_url)

driver = webdriver.Chrome(options=options)
d
```

{% endtab %}
{% endtabs %}

***

### Connection Best Practices

* **Timely Connection**: Connect to the browser task within 5 minutes of creation to prevent automatic termination.
* **Keep Alive**: For longer sessions, enable the `keepAlive` option during session creation.
* **Default Context**: Use the default browser context to ensure compatibility with features like stealth mode.

***

### Controlling the Browser

Once connected, you can control the browser using your framework's API. Common actions include:

* **Navigation**: `page.goto('https://example.com')`
* **Clicking Elements**: `page.click('#submit')`
* **Typing Text**: `page.type('#username', 'myUser')`
* **Extracting Content**: `page.content()`

***

### &#x20;Live View and Session Monitoring

**Browser Swarm** provides real-time monitoring tools:

* **Session Inspector**: View live browser interactions, network requests, console logs, and performance metrics. [->](broken://pages/9S5YYMcnbZYxNAp5Gdnw)
* **Embedded View**: Integrate a live browser view into your application for real-time observation and control. [->](broken://pages/US1ujvX2MbTwNSkqK0Sp)

***

### Ending a Browser Task

To end a browser task:

* **Programmatically**: Use your framework's method, e.g., `browser.close()` or `session.close()`.
* **Automatically**: Tasks will terminate after a specified timeout or upon disconnection, unless `keepAlive` is enabled.

Properly ending tasks ensures resource optimization and prevents unnecessary charges.

***

For more advanced features like file [downloads](broken://pages/3QgwyFh0fBg8fF0IOwYv), [stealth mode](broken://pages/NIbRYk89bVEc26khxWyO), and [proxy integration](broken://pages/f04VF7KfC4oh1LqcbdzU), refer to the respective sections in this documentation.


---

# Agent Instructions: 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://browserswarm.gitbook.io/docs/interacting-with-browser-tasks.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.
