zoukankan      html  css  js  c++  java
  • 《SeleniumBasic 3.141.0.0

    SeleniumBasic支持6种浏览器,每种浏览器的启动之前需要创建“选项”和“服务”。选项主要用于对浏览器的行为进行预设,而服务用于对驱动文件进行预设。

    如果以Edge浏览器为例,那么需要创建EdgeOptions和EdgeDriverService。

    这些浏览器的可用属性和方法,参考如下的XML文件。

    <SeleniumBasic>
        <Chrome>
            <ChromeOptions AcceptInsecureCertificates="" BinaryLocation="C:Program Files (x86)GoogleChromeApplicationchrome.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:SeleniumDrivers" 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:SeleniumDrivers" driverExecutableFileName="MicrosoftWebDriver.exe" port=""/>
                <!--"msedgedriver.exe" rename to "MicrosoftWebDriver.exe"-->
            </EdgeDriverService>
        </Edge>
        <Firefox>
            <FirefoxOptions AcceptInsecureCertificates="" BrowserExecutableLocation="C:Program FilesMozilla Firefoxfirefox.exe" BrowserName="" BrowserVersion="" PlatformName="" UseLegacyImplementation="">
            </FirefoxOptions>
            <FirefoxDriverService BrowserCommunicationPort="" ConnectToRunningBrowser="" FirefoxBinaryPath="C:Program FilesMozilla Firefoxfirefox.exe" HideCommandPromptWindow="True" Host="" HostName="" OpenBrowserToolbox="" Port="" SuppressInitialDiagnosticInformation="">
                <CreateDefaultService driverPath="E:SeleniumDrivers" 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:SeleniumDrivers" 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:SeleniumDrivers" driverExecutableFileName="" port=""/>
            </OperaDriverService>
        </Opera>
        <Safari>
            <SafariOptions AcceptInsecureCertificates="" BrowserName="" BrowserVersion="" EnableAutomaticInspection="" EnableAutomaticProfiling="" IsTechnologyPreview="" PlatformName="">
            </SafariOptions>
            <SafariDriverService HideCommandPromptWindow="True" HostName="" Port="" SuppressInitialDiagnosticInformation="" UseLegacyProtocol="">
                <CreateDefaultService driverPath="E:SeleniumDrivers" 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:SeleniumDrivers"
            .HideCommandPromptWindow = True
        End With
        Set Options1 = New SeleniumBasic.ChromeOptions
        With Options1
            .BinaryLocation = "C:Program Files (x86)GoogleChromeApplicationchrome.exe"
            .AddExcludedArgument "enable-automation"
        End With
        
        Set Service2 = New SeleniumBasic.EdgeDriverService
        With Service2
            .CreateDefaultService driverPath:="E:SeleniumDrivers", 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:SeleniumDrivers"
            .HideCommandPromptWindow = True
        End With
        Set Options3 = New SeleniumBasic.FirefoxOptions
        With Options3
            .BrowserExecutableLocation = "C:Program FilesMozilla Firefoxfirefox.exe"
        End With
    
        Set Service4 = New SeleniumBasic.InternetExplorerDriverService
        With Service4
            .CreateDefaultService driverPath:="E:SeleniumDrivers"
            .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:SeleniumDrivers", driverexecutablefilename:="operadriver.exe"
            .HideCommandPromptWindow = True
        End With
        Set Options5 = New SeleniumBasic.OperaOptions
        With Options5
            .BinaryLocation = "C:UsersAdministratorAppDataLocalProgramsOpera71.0.3770.148opera.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个浏览器的窗口大小和位置进行对齐排列。最后在循环中把每个浏览器退出。

    动态图

  • 相关阅读:
    【学习】Linux Shell脚本编程
    Linux 系统进程相关命令
    Linux 文件权限管理
    Linux 用户关联命令
    Linux shell 及命令汇总
    服务器通过树莓派控制继电器
    华为绩效管理:这样减员、增效、加薪,不服不行!!
    工作十年的程序员,却拿着毕业三年的工资,再不开窍就真晚了!
    员工离职原因,只有两点最真实,其他都是扯淡!
    最全面的2017物联网安全事件盘点
  • 原文地址:https://www.cnblogs.com/ryueifu-VBA/p/13762683.html
Copyright © 2011-2022 走看看