《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。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM