《SeleniumBasic 3.141.0.0 - 在VBA中操作浏览器》系列文章之二十:利用ICapabilities查询浏览器属性
Capabilities类用于修改Web驱动程序的多个属性。Desired capabilities类提供一组键值对key-value,用于更改web驱动程序的各个属性,如浏览器名称、浏览器平台等。
SeleniumBasic中执行如下程序:
Dim Caps As SeleniumBasic.ICapabilities
Set Caps = WD.Capabilities
Debug.Print Caps.ToString
立即窗口打印出一个JSON格式的字符串
{ "acceptInsecureCerts": false, "browserName": "chrome", "browserVersion": "85.0.4183.102", "chrome": { "chromedriverVersion": "85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689})", "userDataDir": "C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\scoped_dir4632_532329042" }, "goog:chromeOptions": { "debuggerAddress": "localhost:49840" }, "networkConnectionEnabled": false, "pageLoadStrategy": "normal", "platformName": "windows", "proxy": {}, "setWindowRect": true, "strictFileInteractability": false, "timeouts": { "implicit": 0, "pageLoad": 300000, "script": 30000 }, "unhandledPromptBehavior": "dismiss and notify", "webauthn:virtualAuthenticators": true }
可以看到包含browserName等键值对。
还可以直接抽取到其中某个信息,例如获取browserVersion,浏览器的版本信息。
Dim s1 As String
s1 = Caps.GetCapability("browserVersion")
Debug.Print Caps.HasCapability("browserVersion")
Dim v As Variant
v = Caps.Item("browserVersion")
Debug.Print v
上述代码,变量s1和v的结果都是85.0.4183.102
如果打开的是其他浏览器,信息也会相应的不同。例如启动Firefox浏览器
WD.New_FirefoxDriver Service:=Service, Options:=Options
WD.URL = "https://www.baidu.com"
Debug.Print WD.Capabilities.ToString
出来的结果是:
{ "acceptInsecureCerts": false, "browserName": "firefox", "browserVersion": "80.0.1", "moz:accessibilityChecks": false, "moz:buildID": "20200831163820", "moz:geckodriverVersion": "0.26.0", "moz:headless": false, "moz:processID": 4560, "moz:profile": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\rust_mozprofilesdDqbl", "moz:shutdownTimeout": 60000, "moz:useNonSpecCompliantPointerOrigin": false, "moz:webdriverClick": true, "pageLoadStrategy": "normal", "platformName": "windows", "platformVersion": "10.0", "rotatable": false, "setWindowRect": true, "strictFileInteractability": false, "timeouts": { "implicit": 0, "pageLoad": 300000, "script": 30000 }, "unhandledPromptBehavior": "dismiss and notify" }