问题
I am using maven here.Here is my Selenium code:
DesiredCapabilities capb = DesiredCapabilities.chrome();
capb.setCapability("chrome.binary","/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless","--disable-gpu", "--no-sandbox","--remote-debugging-port=9222");
capb.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver","/Users/nitinkumar/TEST/chromedriver");
try{
ChromeDriver driver = new ChromeDriver(capb);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://qa.cmnetwork.co");
driver.quit();
}
catch(Exception e)
{
e.printStackTrace();
}
when I run "mvn test" it starts the chrome in GUI mode. but It should open in Headless mode. I have chrome vesrion 59.0, OS X yosemite(10.10.5), chromedriver 2.30 and Selenium 3.4.0.
回答1:
It won't open in GUI mode. Just the chrome launcher icon will be opened. And it is an expected behaviour.
You have to remove the argument --remote-debugging-port
. That will block the launched headless Chrome. So the script will never move forward.And you will get a chrome not reachable
error
So change the arguments like
options.addArguments("--headless","--disable-gpu", "--no-sandbox");
Also, there is no need for --no-sandbox
. As per Official doc only --headless
and --disable-gpu
flags are enough
Unless you have multiple versions of chrome installed, there is no need for DesiredCapabilities
as well.
So the simple code for headless-chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless","--disable-gpu");
System.setProperty("webdriver.chrome.driver","/Users/nitinkumar/TEST/chromedriver");
ChromeDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://qa.cmnetwork.co");
driver.quit();
来源:https://stackoverflow.com/questions/44777876/not-able-to-open-chrome-headless-from-selenium