如果你的電腦中Chrome瀏覽器已經正確安裝,相應的驅動的路徑已經保存到環境變量中,那么SeleniumBasic只需要2行代碼就可以啟動瀏覽器
Private WD As SeleniumBasic.IWebDriver '聲明一個變量
WD.New_ChromeDriver '直接啟動Chrome瀏覽器
實際上,Selenium技術允許在啟動之前設置一些參數,以滿足更多需求。因為瀏覽器一旦啟動起來之后,不能再修改這些參數。
啟動前的設置主要包括兩個方面:
一、瀏覽器選項(通過ChromeOptions設置)
二、驅動服務(通過ChromeDriverService設置)
SeleniumBasic支持的瀏覽器有6個:Chrome、Edge、Firefox、IE、Opera、Safari
每個瀏覽器都有對應的Options和Service設置,假設你要為Edge瀏覽器進行啟動前的設置,聲明如下:
Dim Service As SeleniumBasic.EdgeDriverService
Dim Options As SeleniumBasic.EdgeOptions
其他瀏覽器以此類推。由於Chrome瀏覽器占有最大份額,因此本文都以Chrome為例講解。
首先看一下啟動瀏覽器的New_ChromeDriver方法的構成
Sub New_ChromeDriver([service As ChromeDriverService], [options As ChromeOptions])
該方法后面可以添加兩個可選參數,也就是說啟動瀏覽器的時候,可以使用如下4種方式:
New_ChromeDriver
New_ChromeDriver service
New_ChromeDriver options
New_ChromeDriver service , options
首先看一下如何構造ChromeOptions
Dim Options As SeleniumBasic.ChromeOptions
Set Options = New SeleniumBasic.ChromeOptions
With Options
.BinaryLocation = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
End With
以上代碼在ChromeOptions指明了Chrome瀏覽器的安裝位置。
除了BinaryLocation屬性外,其他屬性和方法還有(標記黃色的為常用):
Property AcceptInsecureCertificates As Boolean
Property BinaryLocation As String
Property BrowserName As String
Property BrowserVersion As String
Property DebuggerAddress As String
Property LeaveBrowserRunning As Boolean
Property MinidumpPath As String
Property PlatformName As String
Property UseSpecCompliantProtocol As Boolean
Sub AddAdditionalCapability(capabilityName As String, capabilityValue, [isGlobalCapability As Boolean = False])
Sub AddArgument(argument As String)
Sub AddEncodedExtension(extension As String)
Sub AddExcludedArgument(argument As String)
Sub AddExtension(pathToExtension As String)
Sub AddWindowType(windowType As String)
Sub EnableMobileEmulation(deviceName As String)
Sub EnableMobileEmulation_deviceSettings(EnableTouchEvents As Boolean, Width As <不支持的變體類型>, Height As <不支持的變體類型>, PixelRatio As Double, UserAgent As String)
接下來看一下如何構造ChromeDriverService
Dim Service As SeleniumBasic.ChromeDriverService
Set Service = New SeleniumBasic.ChromeDriverService
With Service
.CreateDefaultService driverPath:="E:\Selenium\Drivers"
.HideCommandPromptWindow = True
End With
以上代碼,CreateDefaultService方法用於指定驅動文件的位置,該方法包含driverPath和driverExecutableFileName兩個可選參數。
HideCommandPromptWindow=True表示隱藏黑色的命令窗口。
其他屬性和方法還有:
Property AndroidDebugBridgePort As Long
Property EnableVerboseLogging As Boolean
Property HideCommandPromptWindow As Boolean
Property HostName As String
Property IsRunning As Boolean 只讀
Property LogPath As String
Property Port As Long
Property PortServerAddress As String
Property ProcessId As Long 只讀
Property SuppressInitialDiagnosticInformation As Boolean
Property UrlPathPrefix As String
Property WhitelistedIPAddresses As String
Sub CreateDefaultService([driverPath As String], [driverExecutableFileName As String])
Sub Dispose()
Sub Start()
最后給出一個完整的實例
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" '.AddExcludedArgument "enable-automation" '.AddArgument "--start-maximized" '.DebuggerAddress = "127.0.0.1:9999" '不要與其他幾個混用 End With WD.New_ChromeDriver Service:=Service, Options:=Options WD.URL = "https://www.baidu.com" End Sub