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

    如果你的电脑中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)GoogleChromeApplicationchrome.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:SeleniumDrivers"
          .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:SeleniumDrivers"
            .HideCommandPromptWindow = True
        End With
        Set Options = New SeleniumBasic.ChromeOptions
        With Options
            .BinaryLocation = "C:Program Files (x86)GoogleChromeApplicationchrome.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
  • 相关阅读:
    JS函数
    JS数据类型
    JavaScript HTML DOM
    JavaScript-HTML
    HTML基础知识
    数据库--事务:级联删除(学生教师信息表)为例
    javascript window.open
    设置DIV半透明CSS代码:
    css实现你的网页图片半透明效果
    html中,如何打开index.html后可以自动打开另一个页面?
  • 原文地址:https://www.cnblogs.com/ryueifu-VBA/p/13698908.html
Copyright © 2011-2022 走看看