1

ChromeDriverServicechromeOptionsまたはDesiredCapabilitiesとマージして、xvfbでブラウザを実行したいとします。xvfbを使用してヘッドレスのChromeのドライバサービスを希望する機能とマージするにはどうすればよいですか?

以下はコードChromeDriverServiceの一部です。以前はセレニウムグリッドなしで使用しました。

String NodeChromeIncognito = "http://localhost:5558/wd/hub" 

     ChromeDriverService chromeDriverService = new ChromeDriverService.Builder() 
     .usingDriverExecutable(new File("driver_linux/chromedriver")) 
     .usingAnyFreePort() 
     .withEnvironment(ImmutableMap.of("DISPLAY", ":1")).build(); 
    chromeDriverService.start(); 
    // commented because using RemoteWebDriver 
    // driver = new ChromeDriver(chromeDriverService); 

以下私はChromeDriverServiceと合併するRemoteWebDriverの部品コードです。

DesiredCapabilities cap = null; 
String NodeChromeIncognito = "http://localhost:5558/wd/hub"; 
String NodeChrome = "http://localhost:5557/wd/hub"; 
String NodeFirefox = "http://localhost:5556/wd/hub"; 

    if (browserName.equals("chrome")) { 
     cap = DesiredCapabilities.chrome(); 
     cap.setBrowserName("chrome"); 
     driver = new RemoteWebDriver(new URL(NodeChrome), cap); 
    } else if (browserName.equals("firefox")) { 
     System.setProperty("webdriver.gecko.driver", "driver_linux/geckodriver"); 
     cap = DesiredCapabilities.firefox(); 
     cap.setCapability("marionette", true); 
     driver = new RemoteWebDriver(new URL(NodeFirefox), cap); 
    }else if (browserName.equals("chromeIncognito")) { 
     ChromeOptions option = new ChromeOptions(); 
     option.addArguments("--incognito"); 

     cap = DesiredCapabilities.chrome(); 
     cap.setCapability(ChromeOptions.CAPABILITY, option); 
     cap.setPlatform(Platform.WIN10); 
     cap.setBrowserName("chrome incognito"); 
     driver = new RemoteWebDriver(new URL(NodeChromeIncognito), cap); 
    } 

私はクロームのためaddArguments("--headless")を使用することができます知っているが、それは私のWebアプリケーションでうまく動作しません。また、私はDesiredCapabilities.mergeとエラーを使用しました。

コード/設定ChromeDriverServiceChromeOptionsまたはDesiredCapabilitesにマージするにはどうすればよいですか?

+1

ねえMysoundマジック@オーバーフロースタックを歓迎する適切な形式である必要があり、心の問題への投稿に保管してください簡単に理解できます。 – Dilip

答えて

0

あなたがChromeOptionsまたはその両方を達成することができDesiredCapabilitiesChromeDriverServiceをマージしたい言及したように。しかし、現在のSelenium Java Clientリリースとして以下のコンストラクタは廃止されました:

  • オプションA:のみを使用しChromeOptions

    ChromeDriver(Capabilities capabilities) 
    //and 
    ChromeDriver(ChromeDriverService service, Capabilities capabilities) 
    

    がそこで私たちは、次のオプションのいずれかを使用する必要があります

    private static ChromeDriverService service; 
    private WebDriver driver; 
    //code block 
    service = new ChromeDriverService.Builder() 
    .usingDriverExecutable(new File("path/to/my/chromedriver.exe")) 
    .usingAnyFreePort() 
    .build(); 
    chromeDriverService.start(); 
    
    ChromeOptions option = new ChromeOptions(); 
    option.addArguments("--incognito"); 
    driver = new RemoteWebDriver(service.getUrl(), options); 
    
    その後、使用DesiredCapabilitiesChromeOptions以内にマージするMutableCapabilitiesからmerge()を使用します:
  • オプションB

    private static ChromeDriverService service; 
    private WebDriver driver; 
    //code block 
    service = new ChromeDriverService.Builder() 
    .usingDriverExecutable(new File("path/to/my/chromedriver.exe")) 
    .usingAnyFreePort() 
    .build(); 
    chromeDriverService.start();   
    DesiredCapabilities capabilities = new DesiredCapabilities(); 
    capabilities.setCapability("...", true); 
    ChromeOptions option = new ChromeOptions(); 
    option.addArguments("--incognito"); 
    option.merge(capabilities); 
    driver = new RemoteWebDriver(service.getUrl(), options); 
    
+0

あなたのお答えいただきありがとうございます。あなたのコードを試してみました。これはxvfbでブラウザを実行する作業ですが、セレングリッドでは動作しません。私は、ドライバ=新しいRemoteWebDriver(service.getUrl()、options)のために、ノードがノードに移動しないと思う。私のコードではNodeChromeIncognitoを使うからです。これはノードのURLです。 String NodeChromeIncognito = "http:// localhost:5558/wd/hub" –

関連する問題