-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_automation.py
More file actions
47 lines (34 loc) · 1.52 KB
/
browser_automation.py
File metadata and controls
47 lines (34 loc) · 1.52 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
import asyncio
from playwright.async_api import async_playwright
async def main():
# Launch the Playwright browser in headful (visible) mode
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False) # Launch Chromium in headful mode
# Create a new browser context
context = await browser.new_context()
# Create a new page within the context
page = await context.new_page()
try:
# Navigate to Google
await page.goto('https://www.google.com')
# Type "Capsen Lu" into the search input and press Enter
await page.type('textarea[name="q"]', 'Capsen Lu')
await page.press('textarea[name="q"]', 'Enter')
# Wait for search results to load
await page.wait_for_load_state('networkidle')
# Click the first search result link
link = await page.query_selector('a > h3:has-text("Capsen Lu")')
if link:
await link.click()
# Wait for the new page to load
await page.wait_for_load_state('networkidle')
# Take a screenshot of the page
# await page.screenshot(path='capsenlu_profile.png')
# await page.screenshot()
# Keep the browser open for visual inspection
input("Press Enter to close the browser...")
finally:
# Close the browser
await browser.close()
# Run the main function asynchronously
asyncio.run(main())