《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》高级技术之三:SeleniumBasic操作运行中的Chrome浏览器

一般情况下,Selenium只能操作由Selenium创建的浏览器,对于事先打开的浏览器是没办法控制的。

第一步:

在运行对话框中输入cmd,弹出命令提示符。输入命令:

C:\Users\Administrator>"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9999 --user-data-dir="D:\Temp"

 

 按下回车后,手动启动了浏览器。接下来在这个浏览器中打开其他网页,或者干点其他的事情都可以。

 

第二步:

在VBA中设置浏览器选项,注意标记黄色那行。

Private WD As SeleniumBasic.IWebDriver
Sub Baidu()
    On Error GoTo Err1
    Dim Service As SeleniumBasic.ChromeDriverService
    Dim Options As SeleniumBasic.ChromeOptions
    Set WD = New SeleniumBasic.IWebDriver
    Set Service = New SeleniumBasic.ChromeDriverService
    With Service
        .CreateDefaultService driverPath:="E:\Selenium\Drivers"
        .HideCommandPromptWindow = True
    End With
    Set Options = New SeleniumBasic.ChromeOptions
    With Options
        .BinaryLocation = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
        .DebuggerAddress = "127.0.0.1:9999"
    End With
    WD.New_ChromeDriver Service:=Service, Options:=Options
    WD.URL = "https://www.baidu.com"
End Sub

运行上述过程,会发现并没有出现新开的浏览器,而是在你手动打开的浏览器中打开了百度。

支持这种用法的还有Opera。

posted @ 2020-09-20 11:10  ryueifu  阅读(5367)  评论(5编辑  收藏  举报