Skip to content

Commit 3819751

Browse files
init
1 parent 3ec1aac commit 3819751

File tree

2,159 files changed

+433800
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,159 files changed

+433800
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Browserbase Form Fill Examples
2+
3+
This repository contains examples of automating form submissions using Browserbase with different languages and frameworks. These examples accompany the [Browserbase documentation on automating form submissions](/automating-form-submissions).
4+
5+
## Getting Started
6+
7+
1. Clone this repository:
8+
```bash
9+
git clone https://github.com/browserbase/example-form-fill.git
10+
cd browserbase-form-fill-examples
11+
```
12+
13+
2. Choose your preferred language/framework:
14+
15+
### Python (Playwright)
16+
Navigate to the Python implementation:
17+
```bash
18+
cd python
19+
```
20+
Follow the setup instructions in [python/README.md](python/README.md)
21+
22+
### Node.js
23+
Navigate to the Node.js implementation:
24+
```bash
25+
cd node
26+
```
27+
28+
Choose between two approaches:
29+
- **Playwright**: Traditional Playwright implementation with Browserbase
30+
- **Stagehand**: AI-powered automation using Browserbase's Stagehand
31+
32+
Both examples are in TypeScript and demonstrate form automation with different approaches.
33+
34+
## Environment Setup
35+
36+
Each implementation requires Browserbase credentials. Copy the appropriate `.env.example` file:
37+
38+
```bash
39+
cp .env.example .env
40+
```
41+
42+
Then fill in your credentials:
43+
- `BROWSERBASE_API_KEY`: Your Browserbase API key
44+
- `BROWSERBASE_PROJECT_ID`: Your Browserbase project ID
45+
46+
## Examples Overview
47+
48+
Each implementation demonstrates how to:
49+
1. Create a Browserbase session
50+
2. Navigate to the form
51+
3. Fill in form fields
52+
4. Submit the form
53+
5. View the session replay

node/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BROWSERBASE_API_KEY=
2+
BROWSERBASE_PROJECT_ID=
3+
ANTHROPIC_API_KEY=
4+
OPENAI_API_KEY=

node/playwright/formFill.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { chromium } from "playwright-core";
2+
import Browserbase from "@browserbasehq/sdk";
3+
import { config } from "dotenv";
4+
config();
5+
6+
async function createSession() {
7+
const bb = new Browserbase({ apiKey: process.env.BROWSERBASE_API_KEY! });
8+
const session = await bb.sessions.create({
9+
projectId: process.env.BROWSERBASE_PROJECT_ID!,
10+
// Add configuration options here
11+
});
12+
return session;
13+
}
14+
15+
async function fillForm(inputs: any) {
16+
const session = await createSession()
17+
const browser = await chromium.connectOverCDP(session.connectUrl);
18+
19+
// Getting the default context to ensure the sessions are recorded.
20+
const defaultContext = browser.contexts()[0];
21+
const page = defaultContext?.pages()[0];
22+
23+
console.log(`View sessionreplay at https://browserbase.com/sessions/${session.id}`,);
24+
// Navigate to page
25+
await page.goto("https://forms.gle/f4yNQqZKBFCbCr6j7");
26+
27+
// fill superpower
28+
await page.locator(`[role="radio"][data-value="${inputs.superpower}"]`).click();
29+
await page.waitForTimeout(1000);
30+
31+
// fill features_used
32+
for (const feature of inputs.features_used) {
33+
await page.locator(`[role="checkbox"][aria-label="${feature}"]`).click();
34+
}
35+
await page.waitForTimeout(1000);
36+
37+
// fill coolest_build
38+
await page.locator('input[jsname="YPqjbf"]').fill(inputs.coolest_build);
39+
await page.waitForTimeout(1000);
40+
41+
// click submit button
42+
await page.locator('div[role="button"]:has-text("Submit")').click();
43+
44+
// wait 10 seconds
45+
await page.waitForTimeout(10000);
46+
47+
console.log("Shutting down...");
48+
await page.close();
49+
await browser.close();
50+
}
51+
52+
const inputs = {
53+
"superpower": "Invisibility",
54+
"features_used": [
55+
"Stealth Mode",
56+
"Proxies",
57+
"Session Replay"
58+
],
59+
"coolest_build": "A bot that automates form submissions across multiple sites.",
60+
}
61+
fillForm(inputs);

node/stagehand/formFill.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Stagehand } from "@browserbasehq/stagehand";
2+
import { z } from "zod";
3+
import dotenv from "dotenv";
4+
5+
dotenv.config();
6+
7+
async function main() {
8+
const stagehand = new Stagehand({
9+
env: "BROWSERBASE",
10+
verbose: 0,
11+
});
12+
13+
await stagehand.init();
14+
const page = stagehand.page;
15+
16+
async function fillForm(inputs: any) {
17+
// Navigate to the form
18+
await page.goto("https://forms.gle/f4yNQqZKBFCbCr6j7");
19+
20+
// You can use the observe method to find the selector with an act command to fill it in
21+
const superpowerSelector = await page.observe({
22+
instruction: `Find the superpower field: ${inputs.superpower}`,
23+
returnAction: true
24+
});
25+
console.log(superpowerSelector);
26+
await page.act(superpowerSelector[0]);
27+
28+
// You can also explicitly specify the action to take
29+
await page.act({action: "Select the features used: " + inputs.features_used.join(", ")});
30+
await page.act({action: "Fill in the coolest_build field with the following value: " + inputs.coolest_build});
31+
32+
await page.act({action: "Click the submit button"});
33+
await page.waitForTimeout(5000);
34+
35+
// Extract to log the status of the form
36+
const status = await page.extract({instruction: "Extract the status of the form", schema: z.object({status: z.string()})});
37+
console.log(status);
38+
39+
await stagehand.close();
40+
}
41+
42+
const inputs = {
43+
"superpower": "Invisibility",
44+
"features_used": [
45+
"Stealth Mode",
46+
"Proxies",
47+
"Session Replay"
48+
],
49+
"coolest_build": "A bot that automates form submissions across multiple sites.",
50+
}
51+
52+
await fillForm(inputs);
53+
}
54+
55+
main().catch(console.error);

python/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BROWSERBASE_API_KEY=
2+
BROWSERBASE_PROJECT_ID=

python/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Python Form Fill Demo
2+
3+
This demo shows how to automate form filling using Playwright and Browserbase.
4+
5+
## Setup
6+
7+
1. Create and activate a virtual environment:
8+
9+
**Windows:**
10+
```bash
11+
python -m venv venv
12+
venv\Scripts\activate
13+
```
14+
15+
**macOS/Linux:**
16+
```bash
17+
python3 -m venv venv
18+
source venv/bin/activate
19+
```
20+
21+
2. Install dependencies:
22+
```bash
23+
pip install -r requirements.txt
24+
```
25+
26+
3. Set up environment variables:
27+
- Copy `.env.example` to `.env`
28+
- Fill in your API keys:
29+
- `BROWSERBASE_API_KEY`: Your Browserbase API key
30+
- `BROWSERBASE_PROJECT_ID`: Your Browserbase project ID
31+
32+
## Running the Demo
33+
34+
The `form_fill.py` script demonstrates automated form filling. It will:
35+
- Create a new Browserbase session
36+
- Navigate to a Google Form
37+
- Fill in various fields
38+
- Submit the form
39+
40+
Run the script:
41+
```bash
42+
python form_fill.py
43+
```
44+
45+
You can modify the input values in the `inputs` dictionary at the bottom of `form_fill.py` to change the form responses.
46+
47+
The script will print a URL where you can view the session replay on Browserbase.
48+

python/form_fill.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
from playwright.sync_api import sync_playwright
3+
from browserbase import Browserbase
4+
from dotenv import load_dotenv
5+
6+
load_dotenv()
7+
8+
def create_session():
9+
"""Creates a Browserbase session."""
10+
bb = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])
11+
session = bb.sessions.create(
12+
project_id=os.environ["BROWSERBASE_PROJECT_ID"],
13+
# Add configuration options here if needed
14+
)
15+
return session
16+
17+
def fill_form(inputs):
18+
"""Automates form filling using Playwright with Browserbase."""
19+
session = create_session()
20+
print(f"View session at https://browserbase.com/sessions/{session.id}")
21+
22+
with sync_playwright() as p:
23+
browser = p.chromium.connect_over_cdp(session.connect_url)
24+
25+
# Get the default browser context and page
26+
context = browser.contexts[0]
27+
page = context.pages[0]
28+
29+
# Navigate to the form page
30+
page.goto("https://forms.gle/f4yNQqZKBFCbCr6j7")
31+
32+
# Select superpower
33+
page.locator(f'[role="radio"][data-value="{inputs["superpower"]}"]').click()
34+
page.wait_for_timeout(1000)
35+
36+
# Select features used
37+
for feature in inputs["features_used"]:
38+
page.locator(f'[role="checkbox"][aria-label="{feature}"]').click()
39+
page.wait_for_timeout(1000)
40+
41+
# Fill in coolest build
42+
page.locator('input[jsname="YPqjbf"]').fill(inputs["coolest_build"])
43+
page.wait_for_timeout(1000)
44+
45+
# Click submit button
46+
page.locator('div[role="button"]:has-text("Submit")').click()
47+
48+
# Wait 10 seconds
49+
page.wait_for_timeout(10000)
50+
51+
print("Shutting down...")
52+
page.close()
53+
browser.close()
54+
55+
if __name__ == "__main__":
56+
inputs = {
57+
"superpower": "Invisibility",
58+
"features_used": [
59+
"Stealth Mode",
60+
"Proxies",
61+
"Session Replay"
62+
],
63+
"coolest_build": "A bot that automates form submissions across multiple sites.",
64+
}
65+
fill_form(inputs)

python/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
playwright==1.43.0
2+
python-dotenv==1.0.1
3+
browserbase==1.2.0

0 commit comments

Comments
 (0)