通過cdp協議可以實現playwright操控webview。
新建Python窗口工程
修改pip.aardio
修改pip.aardio,并執行,安裝playwright。
//安裝模塊
import process.python.pip;
//process.python.path = "python.exe";/*
安裝模塊。
參數可以用一個字符串指定多個 pip 參數,參數以空格分開。
也可以用多個 aardio 參數指定多個 pip 參數,aardio 自動合并所有參數并自動處理轉義。
*/
process.python.pip("install playwright");//如果指定的模塊未安裝,則調用 pip 安裝
// process.python.pip.require("請指定要安裝的模塊名稱");console.pause();
實現playwright操控webview
import win.ui;
/*DSG{{*/
mainForm = win.form(text="playwright測試";right=957;bottom=708)
mainForm.add()
/*}}*/import web.view2;
//第4個參數為0則開啟遠程端口,取空閑端口號
wb = web.view2(mainForm,,0);import string.templatehtml1 = `
from playwright.sync_api import Playwright, sync_playwright, expectdef run(playwright: Playwright) -> None:# 使用chromium.connect_over_cdp遠程連接webviewbrowser = playwright.chromium.connect_over_cdp("http://127.0.0.1:${port}")context = browser.contexts[0]page = context.pages[0]page.goto("https://www.baidu.com/")page.locator("#kw").click()page.locator("#kw").fill("附加webview測試")page.get_by_role("button", name="百度一下").click()with sync_playwright() as playwright:run(playwright)`
html = string.template(html1).format(port = wb.remoteDebuggingPort;
)import process.python;process.python.version = "3.9.13";
process.python.win32 = true;process.python.exec(html);mainForm.show();
return win.loopMessage();
相關鏈接
https://mp.weixin.qq.com/s/F6sLUiY0v0YWyYDYSO2L4Q