from playwright.sync_api import sync_playwright  
  
def run(playwright):  
    browser = playwright.chromium.launch(headless=False)  # 启动浏览器,headless=False 以便看到浏览器操作  
    context = browser.new_context()  
    page = context.new_page()  
      
    # 导航到网页  
    page.goto('https://www.example.com')  
      
    # 设置截屏路径  
    screenshot_path = './screenshots/example.png'  # 确保目录存在,或者先创建目录  
      
    # 截取屏幕截图并保存到指定路径  
    page.screenshot(path=screenshot_path)  
      
    # 打印截屏路径以确认保存位置  
    print(f"Screenshot saved at: {screenshot_path}")  
      
    # 关闭浏览器  
    browser.close()  
  
with sync_playwright() as playwright:  
    run(playwright)

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐