问题
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.
- Any guidance how to do that?
- 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