欢迎光临散文网 会员登陆 & 注册

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

2022-09-30 23:57 作者:老张你去哪儿  | 我要投稿

写在前面,阿B这里不支持markdown语法,但是我仍然坚持发在这里,如果看着不舒服,请移步其他网页,搜索同名标题内容即可,知乎、CSDN都同步发布了。

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


```Autohotkey

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到脚本所在的文件夹。


```Autohotkey

#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配置携带进去进行网页操作。

```Autohotkey

#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过去就可以了


```Autohotkey

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


Autohotkey 网页自动化浏览器驱动库Rufaydium初步使用的评论 (共 条)

分享到微博请遵守国家法律