I need to get headless chrome to ignore certificate errors. The option is ignored when running in headless mode, and the driver returns empty html body tags when navigating to an https resource.
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
This is how I am configuring my chrome driver.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
DesiredCapabilities cap=DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
chromeHeadlessDriver = new ChromeDriver(cap);
This thread confirms that --ignore-certificate-errors is ignored in headless mode.
They mention about devtool protocol.
Is it something I can invoke from java? Are there any other alternatives?
There is an excellent article on medium.com by sahajamit
and i have tested the below code, it works perfectly fine with self-signed certificate https://badssl.com/
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--headless", "--window-size=1920,1200","--ignore-certificate-errors");
DesiredCapabilities crcapabilities = DesiredCapabilities.chrome();
crcapabilities.setCapability(ChromeOptions.CAPABILITY, options);
crcapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
crcapabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY, "C:\\temp\\chrome\\chromedriver.log");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "C:\\temp\\chrome\\chromedriver.exe");
ChromeDriverService service = null;
try {
service = new ChromeDriverService.Builder()
.usingAnyFreePort()
.withVerbose(true)
.build();
service.start();
} catch (IOException e) {
e.printStackTrace();
}
RemoteWebDriver driver = new RemoteWebDriver(service.getUrl(),crcapabilities);
driver.get("https://self-signed.badssl.com/");
System.out.println(driver.getPageSource());
driver.quit();
software/framework versions
Google Chrome Version 64.0.3282.186
Google Chrome Driver Version 64.0.3282.186
Selenium version 3.11.0
This works for me on ChromeDriver 80.
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
option.AcceptInsecureCertificates = true;
driver = new ChromeDriver(option);
#amila-kumara is working but, usage of DesiredCapabilities.chrome() gives warning to use ChromeOptions. Please see the updated answer.
Set the chrome option values
System.setProperty("webdriver.chrome.driver", Config.NDAC_WEBDRIVER_PATH);
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1920,1200");
options.setAcceptInsecureCerts(true);
options.setHeadless(true);
Start the service
ChromeDriverService service = null;
try {
service = new ChromeDriverService.Builder()
.usingAnyFreePort()
.withVerbose(true)
.build();
service.start();
remoteWebdriverUrl = service.getUrl();
System.out.println("Starting the url " + remoteWebdriverUrl);
} catch (IOException e) {
e.printStackTrace();
}
Note: I was facing the issue while closing the driver(with RemoteWebDriver), chromedriver.exe process won't close even when you use driver.quit(). To fix the issue use ChromeDriver instead of RemoteWebDriver
RemoteWebDriver driver = new ChromeDriver(service, options);
To properly close the driver, use
driver.close();
driver.quit();
Related
I am using the code mentioned below but it is not working Chrome Version 79:
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
//capability.setCapability("pageLoadStrategy", "none");
System.setProperty("webdriver.chrome.driver","driver//chromedriver.exe");
driver = new ChromeDriver(capability);
//driver.manage().deleteAllCookies();
driver.manage().window().maximize();
//Runtime.getRuntime().exec("AutoIT_Exe//AutoIT_Login.exe");
driver.get(url);
You can use switches to ignore such errors
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--ignore-certificate-errors"));
driver = new ChromeDriver(capabilities);
I am running selenium with chromedriver and it all works fine.
Lately I tried to work with 10 chromedrivers simultaneously and it takes all the memory available, so I tried to solve it with headless option in ChromeOptions.
Tried those options:
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1200");
options.addArguments("--ignore-certificate-errors");
But when the program runs with --headless option it won't work. (it worked before without the headless)
this is how I setup my chromeDriver:
private ChromeDriver setupChromeDriver(String proxyAddress, String downloadFilePath){
try{
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilePath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1200");
options.addArguments("--ignore-certificate-errors");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
if(!proxyAddress.equals(""))
cap.setCapability("proxy", setupChromeProxy(proxyAddress));
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().build();
options.merge(cap);
logger.info("Selenium chrome driver set up with updated download location");
return new ChromeDriver(chromeDriverService, options);
}
catch (Exception e){
logger.info(e.getMessage());
logger.info(BaseUtilities.getStackTrace(e));
e.printStackTrace();
return null;
}
}
example how I find element which works without the headless option:
driver.findElement(By.xpath("//input[#id='identifierId']")).sendKeys(dumper.getUsername() + Keys.ENTER);
I am using Windows 10, chrome Version 75.0.3770.142 (Official Build) (64-bit),
ChromeDriver 75.0.3770.140
Tried to find some information in similar posts but couldn't find a working solution.
Thank you.
You can simply try like this
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver wDriver = new ChromeDriver(chromeOptions);
I am using the below code to create a firefox web driver using Selenium Java API.But it neither create firefox instance nor gives any error message.Control comes directly to finally block after some time.
Java APi used - 2.46.0
Firefox version - 32.0.1
JRE -1.8.25
Can some one help me to debug the issue?
code -
WebDriver driver;
try{
ProfilesIni profile = new ProfilesIni(); //ignore ietab+options
FirefoxProfile defualtProfile = profile.getProfile("default");
//defualtProfile.setAcceptUntrustedCertificates(false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox(); //To over come ssl certificate error
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(FirefoxDriver.PROFILE,defualtProfile);
Thread.sleep((long)(2000*Math.random()));
driver=new FirefoxDriver(capabilities);
return driver;
}
catch(Exception ex)
{
logger.error("Exception - > " + ex.toString());
return null;
}
finally
{
logger.info("End");
}
There is simple code to initialize firefox driver. Try below code and let me know.
For more information refer this.
DesiredCapabilities dc=DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
dc.setCapability(FirefoxDriver.PROFILE, profile);
Webdriver driver = new FirefoxDriver(dc);
return driver;
I am using Browsermob proxy to catch HAR content of loaded web page thru chrome webdriver.
<dependency>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core-littleproxy</artifactId>
<version>2.1.0-beta-3</version>
</dependency>
And here is how I initialize proxy:
ChromeOptions options = new ChromeOptions();
for (String extension : DriverProperties.CHROME_EXTENSIONS.getStringArray()) {
options.addExtensions(new File(extension));
}
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE, false);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
proxy = new BrowserMobProxyServer();
proxy.start(0);
proxy.setHarCaptureTypes(CaptureType.RESPONSE_CONTENT, CaptureType.RESPONSE_BINARY_CONTENT);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
proxy.newHar();
ChromeDriver driver = new ChromeDriver(capabilities);
But when I try to get url via https (for exapmle https://screen.yahoo.com) I see "This web page has a redirect loop" in the web browser. Is there any solution for this?
public class Test_One
{
public static void main(String[] args) throws Exception {
ProxyServer server = new ProxyServer(8105);
server.start();
server.setCaptureHeaders(true);
server.setCaptureContent(true);
server.newHar("test");
DesiredCapabilities capabilities = new DesiredCapabilities();
Proxy proxy = server.seleniumProxy();
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 8105);
profile.setPreference("network.proxy.ssl", "localhost");
profile.setPreference("network.proxy.ssl_port", 8105);
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.no_proxies_on", "");
//profile.setProxyPreferences(proxy);
profile.setPreference(key, value)
capabilities.setCapability(FirefoxDriver.PROFILE,profile);
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
Har har1 = server.getHar();
}
}
I'm new to selenium and broswermob. This is my code. Whe I try to execute this and getting error The method setProxyPreferences(Proxy) is undefined for the type FirefoxProfile. How to solve this?
You (generally) don't need to configure the FirefoxProfile settings manually. The Using With Selenium section of the readme file gives an example of how to use the proxy with Selenium:
// start the proxy
ProxyServer server = new ProxyServer(4444);
server.start();
// get the Selenium proxy object
Proxy proxy = server.seleniumProxy();
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);
// create a new HAR with the label "yahoo.com"
server.newHar("yahoo.com");
// open yahoo.com
driver.get("http://yahoo.com");
// get the HAR data
Har har = server.getHar();
If that doesn't work, it may be a problem with your specific version of Firefox and/or Selenium and/or BrowserMob Proxy. What versions are you using? A stack trace with the exact error message would also help.
Try removing profile from capabilities and passing it directly to FirefoxDriver:
driver = new FirefoxDriver(new FirefoxBinary(),profile,capabilities);