基于aardio web.view2库和python playwright包的内嵌浏览器自动化操作

07-09 928阅读

通过cdp协议可以实现playwright操控webview。

基于aardio web.view2库和python playwright包的内嵌浏览器自动化操作

新建Python窗口工程

基于aardio web.view2库和python playwright包的内嵌浏览器自动化操作

修改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.template
html1 = `
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
    # 使用chromium.connect_over_cdp远程连接webview
    browser = 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

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]