Compare commits
2 Commits
17cd46b761
...
8af6709f3b
| Author | SHA1 | Date | |
|---|---|---|---|
| 8af6709f3b | |||
|
|
010975bddc |
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
playwright>=1.44.0
|
||||
41
screenshot.py
Normal file
41
screenshot.py
Normal file
@ -0,0 +1,41 @@
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
|
||||
OUTPUT_FILE: Path = Path("screenshot.png")
|
||||
TARGET_URL: str = "https://linux.org"
|
||||
|
||||
|
||||
async def capture_screenshot(url: str, output_path: Path) -> None:
|
||||
"""Navigate to a URL, wait for the page to load, and save a screenshot.
|
||||
|
||||
Args:
|
||||
url: The URL to navigate to.
|
||||
output_path: The file path where the screenshot will be saved.
|
||||
"""
|
||||
async with async_playwright() as pw:
|
||||
# Launch Chromium in headless mode
|
||||
browser = await pw.chromium.launch(headless=True)
|
||||
page = await browser.new_page(
|
||||
viewport={"width": 1280, "height": 900}
|
||||
)
|
||||
|
||||
# Navigate and wait until network is idle (page fully loaded)
|
||||
await page.goto(url, wait_until="networkidle", timeout=30_000)
|
||||
|
||||
# Persist screenshot to disk
|
||||
await page.screenshot(path=str(output_path), full_page=False)
|
||||
await browser.close()
|
||||
|
||||
print(f"Screenshot saved to {output_path.resolve()}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Entry point: capture a screenshot of the target URL."""
|
||||
asyncio.run(capture_screenshot(TARGET_URL, OUTPUT_FILE))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in New Issue
Block a user