直接上源代码
import multiprocessing
import time
from multiprocessing import Process
from playwright.sync_api import sync_playwright
def run(playwright):
# 使用 Chromium 浏览器运行 设置 headless=False 以打开可视化窗口
browser = playwright.chromium.launch(headless=False, args=["--start-maximized"])
context = browser.new_context(no_viewport=True)
# C:\Users\Administrator\AppData\Local\ms-playwright\chromium-1124
# 最大化窗口
page = context.new_page()
# 导航到登录页面
page.goto("http://192.168.252.23:8643")
force_full_screen(page)
# 在这里填写你的登录信息
username = ''
password = ''
# 填入用户名和密码,并提交表单
page.fill('input[name="loginnum"]', username)
page.fill('input[name="eascword"]', password)
page.click('button[type="submit"]')
# 等待页面加载完成
page.wait_for_load_state()
return page
# 其他操作...
# ...
def force_full_screen(page):
"""
强制浏览器窗口全屏
:param page: Playwright Page 对象
:return:
"""
# 创建一个 CDP 会话
client = page.context.new_cdp_session(page)
# 获取窗口 ID
window_info = client.send("Browser.getWindowForTarget")
window_id = window_info["windowId"]
# 设置窗口状态为全屏
client.send("Browser.setWindowBounds", {
"windowId": window_id,
"bounds": {"windowState": "fullscreen"}
})
def main():
# 运行 Playwright
with sync_playwright() as playwright:
page = run(playwright)
while not page.is_closed():
time.sleep(5)
if __name__ == '__main__':
main()
测试