《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个浏览器的窗口大小和位置进行对齐排列。最后在循环中把每个浏览器退出。

动态图

 

posted @ 2020-10-02 20:45  ryueifu  阅读(2589)  评论(0编辑  收藏  举报