-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (52 loc) · 1.91 KB
/
Copy pathmain.py
File metadata and controls
63 lines (52 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Solve and verify Google's reCAPTCHA demo with Stagehand V4."""
import asyncio
import os
from dotenv import load_dotenv
from stagehand import Stagehand, browserbase
load_dotenv()
async def main() -> None:
api_key = os.environ.get("BROWSERBASE_API_KEY")
if not api_key:
raise RuntimeError("BROWSERBASE_API_KEY is required")
browser = await browserbase.launch(
api_key=api_key,
browser_settings={"solve_captchas": True},
)
try:
stagehand = await Stagehand.create(
browser=browser,
)
try:
pages = await browser.context.pages()
page = pages[0] if pages else await browser.context.new_page()
await page.goto(
"https://google.com/recaptcha/api2/demo",
wait_until="domcontentloaded",
timeout=60_000,
)
print("Waiting for Browserbase captcha solving...")
token = ""
for _ in range(60):
token = await page.locator("#g-recaptcha-response").input_value()
if token:
break
await asyncio.sleep(1)
if not token:
raise RuntimeError("Captcha token was not populated within 60 seconds")
await stagehand.act("Click the Submit button", page=page)
extracted = await stagehand.extract("Extract all text on this page", page=page)
text = extracted.data.extraction
print("Page content after submission:")
print(text)
finally:
await stagehand.close()
finally:
await browser.close()
print("Session closed successfully")
if __name__ == "__main__":
try:
asyncio.run(main())
except Exception as error:
print(f"reCAPTCHA example failed: {error}")
print("Docs: https://docs.stagehand.dev/v4/first-steps/introduction")
raise