《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》高级技术之九:SeleniumBasic设置代理
SeleniumBasic中的Proxy类用来设置代理。使用步骤是先声明和创建Proxy,然后把该变量赋给ChromeOptions,最后启动浏览器。
Private WD As SeleniumBasic.IWebDriver Sub Baidu() On Error GoTo Err1 Dim Service As SeleniumBasic.ChromeDriverService Dim Options As SeleniumBasic.ChromeOptions Dim MyProxy As SeleniumBasic.Proxy Set WD = New SeleniumBasic.IWebDriver Set Service = New SeleniumBasic.ChromeDriverService With Service .CreateDefaultService driverPath:="E:\Selenium\Drivers" .HideCommandPromptWindow = True End With Set MyProxy = New SeleniumBasic.Proxy With MyProxy .Kind = SeleniumBasic.ProxyKind.ProxyKind_Manual .AddBypassAddress "192.168.1.1" .SocksUserName = "yongfu" .SocksPassword = "123456" End With Set Options = New SeleniumBasic.ChromeOptions With Options Set .Proxy = MyProxy End With WD.New_ChromeDriver Service:=Service, Options:=Options WD.URL = "https://www.baidu.com" Debug.Print WD.Capabilities.ToString End Sub
程序的最后,打印浏览器的所有能力,从打印结果中可以看到设置代理成功。
另外一种简单的方式是,通过AddArgument方法。例如:
在Chrome选项中增加参数 .AddArgument "–proxy-server=http://ip:Port"
Dim Options As SeleniumBasic.ChromeOptions Set Options = New SeleniumBasic.ChromeOptions With Options .AddArgument "–proxy-server=http://192.168.1.1:8080" End With WD.New_ChromeDriver Service:=Service, Options:=Options
以上两个方式有什么区别,我也不太懂。欢迎探讨。