How to start Chrome (both normal & incognito modes) and open URL in remote PC (node) using Selenium Grid in Java?

一曲冷凌霜 提交于 2021-02-08 10:40:17

问题


I am trying Selenium Grid in Java and just want to start Chrome in both

  • normal mode and
  • incognito mode

in remote PC (node) and open google.com

I have setup the hub - node connection. I tried this code, but it seems to be wrong.

  1. Any guidance how to do that?
  2. How to use capability.setCapability()? I found some example to start IE and just replaced the word InternetExplorer with Chrome ... Doesn't work.

Thanks.

Not working code:

System.setProperty("webdriver.chrome.driver" , "C:/Users/chromedriver_win32/chromedriver.exe");

WebDriver driver;
    		 
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(ChromeDriver.BINARY, new File("C:\\Program Files (x86)\\chrome.exe").getAbsolutePath());

driver = new RemoteWebDriver(new URL("http://192.168.0.106:1234/wd/hub"), capability);
driver.get("http://google.com");
System.out.println(driver.getTitle());

回答1:


1 - Capabilities: https://sites.google.com/a/chromium.org/chromedriver/capabilities

2 - The "Icognito Mode" is a capability of the Chrome Browser - and it is set like this:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("-incognito");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

// For local usage
driver = new ChromeDriver(capabilities);

// For HUB usage
driver = new RemoteWebDriver(new URL("hub url here"), capabilities);

3 - You need to use backslashes for the system property pointing to your driver executable, also you need to escape those with additional backslahes if you are using JAVA:

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");

4 - For HUB usage, each node needs the chromedriver. Just put it in the same folder as your selenium-server-standalone-3.3.1.jar and start the node like this:

java -Dwebdriver.chrome.driver="./chromedriver.exe" -jar selenium-server-standalone-3.3.1.jar -role node -nodeConfig nodeConfig.json

Of course you need a nodeConfig that allows Chrome Browsers. Here is an example:

{
  "capabilities":
  [
    {
      "browserName": "chrome",
      "maxInstances": 5,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
  "maxSession": 5,
  "port": 5555,
  "register": true,
  "registerCycle": 5000,
  "hub": "http://localhost:4444",
  "nodeStatusCheckTimeout": 5000,
  "nodePolling": 5000,
  "role": "node",
  "unregisterIfStillDownAfter": 60000,
  "downPollingLimit": 2,
  "debug": false,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {}
}



回答2:


**Normal mode:**

WebDriver driver;
driver=new ChromeDriver();

**incognito mode:**

   WebDriver driver;

System.setProperty("webdriver.chrome.driver","C:/Users/chromedriver_win32/chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("-incognito");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        driver=new ChromeDriver(capabilities);


来源:https://stackoverflow.com/questions/42950530/how-to-start-chrome-both-normal-incognito-modes-and-open-url-in-remote-pc-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!