Selenium 在 OSX High Sierra 上使用 Chrome Driver 时遇到问题

Selenium having trouble working with Chrome Driver on OSX High Sierra

我正在尝试使用 Chrome 驱动程序在我的 MacBook Pro 上运行一个简单的 Selenium 示例。

我的Java代码比较简单:

package foobar;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Test {
    public static void main (String args[]) throws MalformedURLException {

        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9515"),DesiredCapabilities.chrome());
        driver.get("http://www.google.com");
    }
}

我在 Java 控制台上收到以下错误:

INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"E55293B5B5CA0A0B5D8D92156BAC0294","isDefault":true},"id":1,"name":"","origin":"://"}
  (Session info: chrome=65.0.3325.181)
  (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.13.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 149 milliseconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'BUBBAs-MacBook-Pro.local', ip: 'fe80:0:0:0:81e:75ee:3428:6a72%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.3', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=/var/folders/g5/zj52b8jx3yl_699rjb3n5fkh0000gn/T/.org.chromium.Chromium.2Fm0pC}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=65.0.3325.181, platform=MAC, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 14133de001c084e2d16c0e7772f60e5d
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
        at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:364)
        at foorbar.Test.main(Test.java:21)

我从控制台启动了 chrome 驱动程序,输出如下

Bubbas-MacBook-Pro:Downloads bubba$ ./chromedriver 
Starting ChromeDriver (v2.9.248307) on port 9515
[warn] kq_init: detected broken kqueue; not using.: Undefined error: 0
/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppleGVA/AppleGVA-11.3.7/Sources/Slices/Driver/AVD_loader.cpp: failed to get a service for display 6 

现在 Chrome 的一个实例确实启动了——见下面的屏幕截图,但它在 URL 文本字段中填写了 "data:,"。

我的 OSX 用户名是:

Darwin Bubbas-MacBook-Pro.local 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64

我已经用谷歌搜索了,但总是找不到答案 -- 感谢您的帮助。

更新:

  1. 我开始怀疑我是否需要在 OSX 的安全和隐私领域允许一些设置以允许 Chrome 驱动程序与 Chrome 对话。

您使用的是年代久远的 chromedriver,可追溯到 2014 年。为此您需要使用最新的 chromedriver

https://chromedriver.storage.googleapis.com/2.37/chromedriver_mac64.zip

https://chromedriver.storage.googleapis.com/index.html