Autohotkey 网页自动化浏览器驱动库Rufaydium初步使用

Autohotkey 是一种线程语言,可以非常方便快捷地自定义各种热键。在使用的过程中我初步掌握了windows桌面程序控制,并产生了浏览器控制的需求。

我最先发现了ahk的chrome.ahk库,这个库最初用的很不错,可以非常方便地控制已经打开chrome。但是过了两天我就 发现无法使用了,总是报错,页面无法运行脚本的安全错误。我看外网上的资料解释说是webscoket能够容许的线程最多6各,后台chrome开启太多的话,就会报错,我按照别人的描述使用代码修改了线程数量限制,仍然无法起作用。来来回回折腾了有四五天,最终发现了另一个库,Rufaydium.ahk。这个库有一个支持四种浏览器,主流的chrome,firefox,opera,edge。在github和youtube上面都有这个库的教程和视频。

Brave := new Rufaydium() ; Brave browser support chromedriver.exe
; New Session will be created using Brave browser, 
Session := Brave.NewSession("C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe")
Brave.Session() ; will always open new Brave session until we reset 
Brave.Capabilities.Resetbinary() ; reset binary to driver default
Brave.Session() ; will create Chrome session as we have loaded Chrome driver

目前我发现这个库有几个缺点,1:如果使用自己的配置文件,就必须关闭其他的chrome实例;2:如果控制firefox,在调用newsession函数的时候需要把firefox.exe路径写进去,而且经常报错,用的比较少,也就没怎么研究了。

https://github.com/Xeo786/Rufaydium-Webdriver
https://www.youtube.com/c/JoeGlines-Automator/videos

官网的实例如下,实现的功能是定义了F1和F12快捷键,初次运行会自动下载chromedriver.exe到脚本所在的文件夹。

#Include Rufaydium.ahk
/*
	Load "chromedriver.exe" from "A_ScriptDir"
	In case Driver is not yet available, it will Download "chromedriver.exe" into "A_ScriptDir"
	before starting the Driver.
*/
Chrome := new Rufaydium("chromedriver.exe")
f1::
/*
	Create new session if WebBrowser Version Matches the Webdriver Version.
	It will ask to download the compatible WebDriver if not present.
*/
Page := Chrome.NewSession()
; navigate to url
Page.Navigate("https://www.autohotkey.com/")
return

f12::
Chrome.QuitAllSessions() ; close all session 
Chrome.Driver.Exit() ; then exits driver
return

下面是我写的一个简单实例,可以将自己的一些chrome配置携带进去进行网页操作。

#Include D:\Rufaydium\Rufaydium.ahk  ;这里是自己的路径
/*
	Load "chromedriver.exe" from "A_ScriptDir"
	In case Driver is not yet available, it will Download "chromedriver.exe" into "A_ScriptDir"
	before starting the Driver.
    这个包在首次运行的时候会把chrome对应的驱动下载下来
*/
;使用的结果就是chrome最好用
Chrome := new Rufaydium("chromedriver.exe")
; Chrome.capabilities.setUserProfile("Default","C:\Users\xxx\AppData\Local\Google\Chrome\User Data")  ;设置配置文件,使用自己的配置
Page1:=Chrome.newsession()
Page1.Navigate("http://baidu.com")   ;给网页网址
Page1.CDP.WaitFOrLoad()
sleep 3000  ;等待
Chrome.QuitAllSessions() ; closing all session one by one
Chrome.driver.exit() ; exitting driver
ExitApp
return

如果需要填写网页内容,可以使用sendKey这个函数,将element定位以后,将需要填写的内容send过去就可以了

Element := Page1.getElementsbyClassName("edit_lobo_cell")
        p=% Element.Count()
        If (p=2)
            {
            MsgBox,已经登录
            }
        Else 
            {
            Element[2].SendKey("2xxs`n")    ;这里是我的账号
            Element[3].SendKey("xxxx`n")   ;这个元素对应的是我的密码
            }
        Sleep, 500

当然,和chrome.ahk一样,click、close函数也是支持的。更多内容请移步官网自行学习或评论区讨论。

https://github.com/Xeo786/Rufaydium-Webdriver

https://www.youtube.com/c/JoeGlines-Automator/videos

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值