2017-01-24 27 views
6

我想使用鉻無頭自動測試使用硒。 (https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md如何使用硒連接到鉻無頭

我有無頭版本已經在9222.運行,所以如果我打開http://10.252.100.33:9222/json/I做得到

[ { 
    "description": "", 
    "devtoolsFrontendUrl": "/devtools/inspector.html?ws=127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91", 
    "id": "0261be06-1271-485b-bdff-48e443de7a91", 
    "title": "The Chromium Projects", 
    "type": "page", 
    "url": "https://www.chromium.org/", 
    "webSocketDebuggerUrl": "ws://127.0.0.1:9223/devtools/page/0261be06-1271-485b-bdff-48e443de7a91" 
} ] 

作爲下一步我想硒連接到無頭鉻。但是當我嘗試

final DesiredCapabilities caps = DesiredCapabilities.chrome(); 
final WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9222/json"), caps); 
driver.get("http://www.google.com"); 

我得到以下注銷

Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFORMATION: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end 
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFORMATION: Falling back to original OSS JSON Wire Protocol. 
Jän 24, 2017 7:14:45 PM org.openqa.selenium.remote.ProtocolHandshake createSession 
INFORMATION: Falling back to straight W3C remote end connection 

org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{browserName=chrome, version=, platform=ANY}], required capabilities = Capabilities [{}] 
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700' 
System info: host: 'Geralds-MacBook-Pro.local', ip: '192.168.0.249', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.2', java.version: '1.8.0_111' 
Driver info: driver.version: RemoteWebDriver 

的問題是:

+0

你得到這個工作的細節?我在一個階段做過,類似於你所做的(我也使用過'RemoteWebDriver'),但是一個月後我又回到了我的設置,它沒有連接。 – Nikki

+0

爲了讓事情運行,我們切換到了phantomJS。通過使用Selenium的PhantomJSDriver和WebWire協議,我們可以使用此設置。 (聯合國)很幸運,我們必須再次調查,因爲幻像JS維護者正在退出/ Chrome 59支持無頭。 (https://groups.google.com/forum/#!topic/phantomjs/9aI5d-LDuNE) –

回答

11

我覺得自述是豆蔻有點誤導。你不必自己啓動鉻,你可以使用RemoteWebDriver。確保安裝了鍍鉻驅動器(https://sites.google.com/a/chromium.org/chromedriver/home)。

  • 開始chromedriver(如./chromedriver./chromedriver --port=9515
  • 然後,你必須告訴chromedriver,而不是使用鉻
  • 添加 「--headless」 作爲附加參數鉻

代碼應該看起來像這個:

final ChromeOptions chromeOptions = new ChromeOptions(); 
chromeOptions.setBinary("/usr/bin/chromium-browser"); 
chromeOptions.addArguments("--headless"); 
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); 
WebDriver driver = new RemoteWebDriver(url, desiredCapabilities); 

在Ubuntu Linux上爲我工作。

+1

這也適用於我,謝謝!現在要修復'鍵盤轉換需要X顯示器,請考慮使用Xvfb異常! – Nikki

+0

對於其他人: '$ sudo易於得到安裝xvfb' '$ xvfb的運行/家庭/尼克/ chromedriver --whitelisted-ips' – Nikki

+0

https://stackoverflow.com/questions/47067188/headless-chrome-用硒運行 – ses

2

或者,如果您在本地運行它,則可以這樣做。在斯卡拉。

val chromeOptions = new ChromeOptions 
chromeOptions.addArguments("--headless") 
new ChromeDriver(chromeOptions)