谷歌浏览器自动化之百度搜图
By
terrorist
at 2022-04-29 • 0人收藏 • 1369人看过
import win.ui; /*DSG{{*/ var winform = win.form(text="百度搜图";right=287;bottom=103) winform.add( button={cls="button";text="运行";left=88;top=48;right=216;bottom=88;z=1} ) /*}}*/ //io.open() import aaz.chrome.dp; var cdp, err = aaz.chrome.dp() if(!cdp){ winform.msgboxErr(err); return ; } cdp.timeout = 20000; var task = function(path){ // 打开浏览器 var ok, err = cdp.open() if(!ok){ io.print("打开浏览器失败", err); return ; } // 连接浏览器 var ok, err = cdp.connect(); if(!ok){ io.print("连接浏览器失败", err); return ; } // 订阅 Page 事件 cdp.Page.enable(); // 打开网址 cdp.Page.navigate( url = "https://www.baidu.com"; ) // 等待页面加载完成 cdp.waitEvent( "Page.loadEventFired"); // 点击相机 cdp.Runtime.evaluate( expression = /** document.querySelector(".soutu-btn").click(); **/ ) // 获取上传的 input 元素,选择器也可以用 [type=file] var ret = cdp.DOM.querySelector( nodeId = cdp.DOM.getDocument().root.nodeId; selector = ".upload-pic"; ) // 填写文件路径 cdp.DOM.setFileInputFiles( files = {path}; nodeId = ret.nodeId ) } import fsys.dlg winform.button.oncommand = function(id,event){ var path = fsys.dlg.open("图片文件|*.jpg") if(path){ cdp.run(task,,,path) } } winform.show(); win.loopMessage();
相关库:
https://github.com/btx638/Chrome-DevTools-Protocol-aardio
参考:
https://github.com/cyrus-and/chrome-remote-interface/issues/164
4 个回复 | 最后更新于 2022-08-18
模拟一个等待函数
var js = /** const sleep = (time) => { return new Promise(resolve => setTimeout(resolve, time)) } async function sleepAsync(time) { await sleep(time) } sleepAsync(%s) **/ var wait = function(ms){ return cdp.Runtime.evaluate( awaitPromise = true; // 执行是否应该等待结果值,并在等待的承诺被解决后返回。 expression = string.format(js, ms) ) }
测试等待5000毫秒
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=385;bottom=140) winform.add( button={cls="button";text="运行";left=142;top=43;right=221;bottom=83;z=1} ) /*}}*/ io.open() import aaz.chrome.dp; var cdp, err = aaz.chrome.dp() if(!cdp){ winform.msgboxErr(err); return ; } var js = /** const sleep = (time) => { return new Promise(resolve => setTimeout(resolve, time)) } async function sleepAsync(time) { await sleep(time) } sleepAsync(%s) **/ var wait = function(ms){ return cdp.Runtime.evaluate( awaitPromise = true; // 执行是否应该等待结果值,并在等待的承诺被解决后返回。 expression = string.format(js, ms) ) } cdp.timeout = 20000; var task = function(){ // 打开浏览器 var ok, err = cdp.open() if(!ok){ io.print("打开浏览器失败", err); return ; } // 连接浏览器 var ok, err = cdp.connect(); if(!ok){ io.print("连接浏览器失败", err); return ; } // 订阅 Page 事件 cdp.Page.enable(); // 打开网址 cdp.Page.navigate( url = "https://www.baidu.com"; ) // 等待页面加载完成 cdp.waitEvent( "Page.loadEventFired" ); io.print("等待开始") var tick = time.tick() wait(5000) io.print("等待结束,用时:", time.tick()-tick ) // 关闭浏览器 cdp.Browser.close(); } winform.button.oncommand = function(id,event){ cdp.run( task, function(){ winform.button.disabled = true }, function(){ winform.button.disabled = false } ) } winform.show(); win.loopMessage();
登录后方可回帖
截图淘宝首页
参考:
https://dschnurr.medium.com/using-headless-chrome-as-an-automated-screenshot-tool-4b07dffba79a
https://blog.csdn.net/z69183787/article/details/91984005
https://dev.to/gdledsan/selenium-4-and-chrome-driver-take-full-page-screenthos-2j8d
https://github.com/puppeteer/puppeteer/blob/main/src/common/Page.ts#L2741