2015-12-08 40 views
1

我使用selenium和chromedriver來控制debian linux下的一些chrome實例。我想使這些chrome實例無頭,我也想通過具有不同顯示分辨率的Xvfb在不同的虛擬顯示器上啓動它們。如何構建一個類似二進制文件的google-chrome包裝腳本?

我的java/selenium代碼直接使用chrome二進制工作(非無頭)。嚴格來說,chrome二進制文件(/ usr/bin/google-chrome)也只是路徑中的一個包裝腳本。用我自制的鍍鉻包裝腳本(並將其設置爲二進制的chromeOptions,喜歡這裏描述:https://sites.google.com/a/chromium.org/chromedriver/capabilities)它不工作和失敗:unknown error: Chrome failed to start: exited abnormally

我想原因是我的包裝腳本。什麼是錯,或我應該改進什麼? 這是腳本:

#!/bin/bash 
export DISPLAY=:1920 
cd /usr/bin/ 
google-chrome "[email protected]" 

的java的代碼將通過chromeOptions包裝腳本鉻二進制:

// set custom binary 
ChromeOptions options = new ChromeOptions(); 
options.setBinary("/path/to/executable-wrapper-script"); 

請提醒的是,Xvfb來顯示正在運行,所以這不是問題。我用x11vnc測試了它們,並且我也開始使用chrome,直接在shell中調用包裝器腳本。

回答

1

我解決了主要問題,所以問題中的解決方法不再需要。如前所述(https://groups.google.com/forum/#!topic/chromedriver-users/aFVdnfN0_HI)我現在可以爲每個chrome實例設置DISPLAY變量來控制應該在哪個顯示器上啓動實例。

只需使用下列方法之一來設置顯示環境變量:

ChromeDriverService chromeDriverService = new ChromeDriverService.Builder() 
          .usingDriverExecutable(new File(ChromeUtils.getChromeDriverPath())) 
          .usingAnyFreePort() 
          .withEnvironment(ImmutableMap.of("DISPLAY",":1024")) 
          .build(); 

        try { 
         chromeDriverService.start(); 
         webDriver = new ChromeDriver(chromeDriverService, caps); 
        } .... 

或使用chromeOptions一個簡單的論點:

args.add("--display=:1024"); 
chromeOptions.addArguments(args); 
相關問題