26 lines
581 B
Python
26 lines
581 B
Python
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
def test_website():
|
|
with sync_playwright() as p:
|
|
# 启动浏览器
|
|
browser = p.chromium.launch(headless=True) # headless=False 显示浏览器窗口
|
|
page = browser.new_page()
|
|
|
|
# 访问网页
|
|
page.goto("https://www.baidu.com")
|
|
|
|
# 截图
|
|
page.screenshot(path="example.png")
|
|
|
|
# 获取页面标题
|
|
title = page.title()
|
|
print(f"页面标题: {title}")
|
|
|
|
# 关闭浏览器
|
|
browser.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_website()
|