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

admin2024-07-06  21

通过cdp协议可以实现playwright操控webview。
基于aardio web.view2库和python playwright包的内嵌浏览器自动化操作,1,第1张

新建Python窗口工程

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

修改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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!