《SeleniumBasic 3.141.0.0 - 在VBA中操作瀏覽器》系列文章之十四:同時啟動多個瀏覽器


SeleniumBasic支持6種瀏覽器,每種瀏覽器的啟動之前需要創建“選項”和“服務”。選項主要用於對瀏覽器的行為進行預設,而服務用於對驅動文件進行預設。

如果以Edge瀏覽器為例,那么需要創建EdgeOptions和EdgeDriverService。

這些瀏覽器的可用屬性和方法,參考如下的XML文件。

<SeleniumBasic>
    <Chrome>
        <ChromeOptions AcceptInsecureCertificates="" BinaryLocation="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" BrowserName="" BrowserVersion="" DebuggerAddress="" PlatformName="">
            <AddArgument argument="--disable-infobars"/>
            <!--can be duplicate-->
            <EnableMobileEmulation deviceName="">
                <ChromeMobileEmulationDeviceSettings EnableTouchEvents="" Width="" Height="" PixelRatio="" UserAgent=""/>
            </EnableMobileEmulation>
        </ChromeOptions>
        <ChromeDriverService HideCommandPromptWindow="True" HostName="" LogPath="" Port="" PortServerAddress="" UrlPathPrefix="" WhitelistedIPAddresses="">
            <CreateDefaultService driverPath="E:\Selenium\Drivers" driverExecutableFileName="chromedriver.exe"/>
        </ChromeDriverService>
    </Chrome>
    <Edge>
        <EdgeOptions AcceptInsecureCertificates="" BrowserName="" BrowserVersion="" PlatformName="" StartPage="" UseInPrivateBrowsing="">
        </EdgeOptions>
        <EdgeDriverService HideCommandPromptWindow="True" Host="" HostName="" Package="" Port="" SuppressInitialDiagnosticInformation="" UseSpecCompliantProtocol="" UseVerboseLogging="">
            <CreateDefaultService driverPath="E:\Selenium\Drivers" driverExecutableFileName="MicrosoftWebDriver.exe" port=""/>
            <!--"msedgedriver.exe" rename to "MicrosoftWebDriver.exe"-->
        </EdgeDriverService>
    </Edge>
    <Firefox>
        <FirefoxOptions AcceptInsecureCertificates="" BrowserExecutableLocation="C:\Program Files\Mozilla Firefox\firefox.exe" BrowserName="" BrowserVersion="" PlatformName="" UseLegacyImplementation="">
        </FirefoxOptions>
        <FirefoxDriverService BrowserCommunicationPort="" ConnectToRunningBrowser="" FirefoxBinaryPath="C:\Program Files\Mozilla Firefox\firefox.exe" HideCommandPromptWindow="True" Host="" HostName="" OpenBrowserToolbox="" Port="" SuppressInitialDiagnosticInformation="">
            <CreateDefaultService driverPath="E:\Selenium\Drivers" driverExecutableFileName="geckodriver.exe"/>
        </FirefoxDriverService>
    </Firefox>
    <IE>
        <InternetExplorerOptions AcceptInsecureCertificates="" BrowserCommandLineArguments="" BrowserName="" BrowserVersion="" EnableNativeEvents="" EnablePersistentHover="" EnsureCleanSession="" ForceCreateProcessApi="" ForceShellWindowsApi="" IgnoreZoomLevel="" InitialBrowserUrl="" IntroduceInstabilityByIgnoringProtectedModeSettings="" PlatformName="" RequireWindowFocus="" UsePerProcessProxy="">
        </InternetExplorerOptions>
        <InternetExplorerDriverService HideCommandPromptWindow="True" Host="" HostName="" LibraryExtractionPath="" LogFile="" Port="" SuppressInitialDiagnosticInformation="" WhitelistedIPAddresses="">
            <CreateDefaultService driverPath="E:\Selenium\Drivers" driverExecutableFileName="IEDriverServer.exe"/>
        </InternetExplorerDriverService>
    </IE>
    <Opera>
        <OperaOptions AcceptInsecureCertificates="" BinaryLocation="" BrowserName="" BrowserVersion="" DebuggerAddress="" LeaveBrowserRunning="" MinidumpPath="" PlatformName="">
            <AddArgument argument="--disable-infobars"/>
            <!--can be duplicate-->
        </OperaOptions>
        <OperaDriverService AndroidDebugBridgePort="" EnableVerboseLogging="" HideCommandPromptWindow="True" HostName="" LogPath="" Port="" PortServerAddress="" SuppressInitialDiagnosticInformation="" UrlPathPrefix="">
            <CreateDefaultService driverPath="E:\Selenium\Drivers" driverExecutableFileName="" port=""/>
        </OperaDriverService>
    </Opera>
    <Safari>
        <SafariOptions AcceptInsecureCertificates="" BrowserName="" BrowserVersion="" EnableAutomaticInspection="" EnableAutomaticProfiling="" IsTechnologyPreview="" PlatformName="">
        </SafariOptions>
        <SafariDriverService HideCommandPromptWindow="True" HostName="" Port="" SuppressInitialDiagnosticInformation="" UseLegacyProtocol="">
            <CreateDefaultService driverPath="E:\Selenium\Drivers" driverExecutableFileName=""/>
        </SafariDriverService>
    </Safari>
</SeleniumBasic>

接下來講述一下每種瀏覽器的Selenium環境構建方法。

 

最后講一下在SeleniumBasic中的代碼實現。

Private WD(1 To 5) As SeleniumBasic.IWebDriver
Private Service1 As SeleniumBasic.ChromeDriverService, Options1 As SeleniumBasic.ChromeOptions
Private Service2 As SeleniumBasic.EdgeDriverService, Options2 As SeleniumBasic.EdgeOptions
Private Service3 As SeleniumBasic.FirefoxDriverService, Options3 As SeleniumBasic.FirefoxOptions
Private Service4 As SeleniumBasic.InternetExplorerDriverService, Options4 As SeleniumBasic.InternetExplorerOptions
Private Service5 As SeleniumBasic.OperaDriverService, Options5 As SeleniumBasic.OperaOptions

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

Sub 啟動五個瀏覽器()
    On Error GoTo Err1
    Dim ScreenWidth As Long, ScreenHeight As Long
    ScreenWidth = GetSystemMetrics(0&)
    ScreenHeight = GetSystemMetrics(1&)
    Set Service1 = New SeleniumBasic.ChromeDriverService
    With Service1
        .CreateDefaultService driverPath:="E:\Selenium\Drivers"
        .HideCommandPromptWindow = True
    End With
    Set Options1 = New SeleniumBasic.ChromeOptions
    With Options1
        .BinaryLocation = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
        .AddExcludedArgument "enable-automation"
    End With
    
    Set Service2 = New SeleniumBasic.EdgeDriverService
    With Service2
        .CreateDefaultService driverPath:="E:\Selenium\Drivers", driverexecutablefilename:="MicrosoftWebDriver.exe"
        .HideCommandPromptWindow = True
    End With
    Set Options2 = New SeleniumBasic.EdgeOptions
    With Options2
    End With

    Set Service3 = New SeleniumBasic.FirefoxDriverService
    With Service3
        .CreateDefaultService driverPath:="E:\Selenium\Drivers"
        .HideCommandPromptWindow = True
    End With
    Set Options3 = New SeleniumBasic.FirefoxOptions
    With Options3
        .BrowserExecutableLocation = "C:\Program Files\Mozilla Firefox\firefox.exe"
    End With

    Set Service4 = New SeleniumBasic.InternetExplorerDriverService
    With Service4
        .CreateDefaultService driverPath:="E:\Selenium\Drivers"
        .HideCommandPromptWindow = True
    End With
    Set Options4 = New SeleniumBasic.InternetExplorerOptions
    With Options4
        .IntroduceInstabilityByIgnoringProtectedModeSettings = True
    End With

    Set Service5 = New SeleniumBasic.OperaDriverService
    With Service5
        .CreateDefaultService driverPath:="E:\Selenium\Drivers", driverexecutablefilename:="operadriver.exe"
        .HideCommandPromptWindow = True
    End With
    Set Options5 = New SeleniumBasic.OperaOptions
    With Options5
        .BinaryLocation = "C:\Users\Administrator\AppData\Local\Programs\Opera\71.0.3770.148\opera.exe"
    End With

    Set WD(1) = New SeleniumBasic.IWebDriver
    Set WD(2) = New SeleniumBasic.IWebDriver
    Set WD(3) = New SeleniumBasic.IWebDriver
    Set WD(4) = New SeleniumBasic.IWebDriver
    Set WD(5) = New SeleniumBasic.IWebDriver
    WD(1).New_ChromeDriver Service:=Service1, Options:=Options1
    WD(2).New_EdgeDriver Service:=Service2, Options:=Options2
    WD(3).New_FirefoxDriver Service:=Service3, Options:=Options3
    WD(4).New_InternetExplorerDriver Service:=Service4, Options:=Options4
    WD(5).New_OperaDriver Service:=Service5, Options:=Options5
    
    WD(1).URL = "https://www.baidu.com"
    WD(2).URL = "https://www.baidu.com"
    WD(3).URL = "https://www.baidu.com"
    WD(4).URL = "https://www.baidu.com"
    WD(5).URL = "https://www.baidu.com"
    
    WD(1).Manage.Window.SetPositon 0, 0
    WD(1).Manage.Window.SetSize ScreenWidth / 2, ScreenHeight / 2
    WD(2).Manage.Window.SetPositon ScreenWidth / 2, 0
    WD(2).Manage.Window.SetSize ScreenWidth / 2, ScreenHeight / 2
    WD(3).Manage.Window.SetPositon 0, ScreenHeight / 2
    WD(3).Manage.Window.SetSize ScreenWidth / 2, ScreenHeight / 2
    WD(4).Manage.Window.SetPositon ScreenWidth / 2, ScreenHeight / 2
    WD(4).Manage.Window.SetSize ScreenWidth / 2, ScreenHeight / 2
    WD(5).Manage.Window.SetPositon ScreenWidth / 4, ScreenHeight / 4
    WD(5).Manage.Window.SetSize ScreenWidth / 2, ScreenHeight / 2
    
    WD(1).FindElementById("kw").SendKeys "Chrome"
    WD(2).FindElementById("kw").SendKeys "Edge"
    WD(3).FindElementById("kw").SendKeys "Firefox"
    WD(4).FindElementById("kw").SendKeys "IE"
    WD(5).FindElementById("kw").SendKeys "Opera"
    Dim i As Integer
    For i = 1 To 5
        Debug.Print WD(i).Title, WD(i).URL
    Next i
    Stop
    For i = 1 To 5
        WD(i).Quit
    Next i
    Exit Sub
Err1:
    MsgBox Err.Description, vbCritical
End Sub

代碼分析:利用API函數算出屏幕的寬度和高度,然后把5個瀏覽器的窗口大小和位置進行對齊排列。最后在循環中把每個瀏覽器退出。

動態圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM