chromedriver, how to use predefind settings - google-chrome

I am using Chrome as my Selenium test environment. Currently it is chrome 67 and chromedriver 2.40
Sometimes during the test the browser is closed. When it happens I reopen the browser using the chromedriver.
The problem is that I loose the tab the test was testing. Is there a way to use "On startup" setting with the checkbox "Continue where you left off"?

Found the answer.
Each time chromedriver opens a browser, it creates new profile. The profile overwrites all the chrome settings. "Continue where you left off" option is part of the profile.
The solution is:
private static WebDriver startChrome() {
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=c:\\temp\\chromeProf"); // Set non-default profile
Map<String, Object> prefs = new HashMap<>();
prefs.put("session.restore_on_startup", 1); // Edit profile preferences to be "Continue where you left off"
options.setExperimentalOption("prefs", prefs); // Set preferences to ChromeOptions
WebDriver driver = new ChromeDriver(options); // Start driver with those options
driver.manage().window().maximize();
return driver;
}

Related

Looking For Ways To Update Chromedriver Options

I have an application that tests various custom Chrome extensions with various sites. My current code looks like this
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("extension1.zip"));
WebDriver driver = new ChromeDriver(options);
driver.get("https://site1.com");
// Evaluate page
driver.quit();
options = new ChromeOptions();
options.addExtensions(new File("extension2.zip"));
driver = new ChromeDriver(options);
driver.get("https://site2.com");
// Evaluate page
driver.quit();
There is a lot of overhead associated with opening and closing the browser. Over time, the application gets slower and slower. I would like to be able to remove and add extensions without opening and closing the driver and the browser. Each site needs to be evaluated with only one of the extensions. Currently, I'm evaluating 8 sites and 8 extensions, but the number of sites and extensions will grow over time.
Any help is appreciated.
See if this will remove previously added extensions:
options.addArguments("--disable-extensions");
The entire code is
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);

After copying setted up Chrome profile to another computer it misses cookies and installed extensions

I set up a Chrome profile with extension and some authorization cookies. It works good on my computer if I run it from the code. But when I copy this profile to another computer and run it from the code, then all the extensions and cookies of this profile are missing.
Why is it so? And how can I deal with it?
It is expected behavior.
See this code to launch browser with extension :
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", "F:\\Automation\\chromedriver.exe");
String pathToExtension = "C:\\Users\\USER_DELL_2014_07\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3\\Extensions\\bhlhnicpbhignbdhedgjhgdocnmhomnp\\2.0_0";
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=" + pathToExtension);
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
}
pathToExtension which have value : C:\\Users\\USER_DELL_2014_07\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3\\Extensions\\bhlhnicpbhignbdhedgjhgdocnmhomnp\\2.0_0
This path will be different for second machine because of USER_DELL_2014_07 this.
Hope this will be helpful.

Selenium Java ChromeDriver - How to disable attached sites

When loading a web page, it sometimes keep hanging on loading sites like "google analytics", "gstatic.com", etc.
It sometimes hangs forever.
Is there a way to disable this behavior?
Working with Selenium 3.4.0 along with the latest chromedriver 2.29 & latest Google Chrome 58.0 to restrict the loading of the sites you can take help of pageLoadStrategy through DesiredCapabilities Class as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
DesiredCapabilities c1 = DesiredCapabilities.chrome();
c1.setCapability(ChromeOptions.CAPABILITY, options);
c1.setCapability("pageLoadStrategy", "none");
WebDriver driver1 = new ChromeDriver(c1);
Navigation navigate = driver1.navigate();
navigate.to("https://gmail.com");
Disclaimer:
Using this capability you cannot be sure if the HTML DOM have completely loaded or not for you to continue working with the WebElements present on that Webpage.
Let me know if this Answers your Question.

Getting chrome.exe popup opened while executing selenium scripts

I'm getting a popup opened from location C:\Program Files\Google\Chrome\application\chrome.exe while executing selenium webdriver scripts in chrome browser.
This one is throwing the error as session timed out.
Note: The same codebase is working fine in other machine.
Can you please help me out to get this sorted.
The code I am using is as below:-
var arr = new string[7] {
"--start-maximized", "--ignore-certificate-errors", "--disable-popup-blocking", "--disable-default-apps", "--auto-launch-at-startup", "--always-authorize-plugins", "--user-agent= " + FrameGlobals.userAgentValue
};
chromeCapabilities.AddArguments(arr);
WebDriverObj = new ChromeDriver(chromeCapabilities);
This is how i'm initiating the chrome browser. not mentioning any version inside codebase.
enter image description here
Thanks in advance.
Hema
You can add all your argument one by one and then pass it to the Chromedriver as below:-
WebDriver driver=null;
System.setProperty("webdriver.chrome.driver","./src//lib//chromedriver");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArgument("--start-maximized");
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
capabilities.setCapability("chrome.binary","./src//lib//chromedriver");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com/");
Replace your argument with above-mentioned arguments
Hope it will help you :)

Selenium Chrome suppress/dismiss client certificate selection dialog

How can I suppress or automatically dismiss the client certificate selection dialog with selenium (chrome driver)?
I can't use this certificate, because it is stored on a chip card and I would have to enter a PIN. If no card is available, our website used a credential based login and I want to test this.
I found a solution to this problem:
You must use chrome parameter - AutoSelectCertificateForUrls
Add this to the windows registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls\1 = "{"pattern":"https://yoursite.com","filter":{}}"
In linux you need this file set:
$HOME/etc/opt/chrome/policies/managed/auto_select_certificate.json
With this content:
{
"AutoSelectCertificateForUrls": [ "{\"pattern\":\"*\",\"filter\":{}}" ]
}
With this set it should allow every installed client certificate automatically.
Detailed article about how to solve it in C# with Docker can be found in an article I wrote here:
https://sgedda.medium.com/running-selenium-with-chromedriver-together-with-client-certificate-set-in-headful-mode-with-net-a79bde19e472
Try to launch chrome using "--ignore-certificate-errors" and "--ignore-urlfetcher-cert-requests" arguments.
ChromeOptions opts = new ChromeOptions();
opts.addArguments("ignore-certificate-errors","ignore-urlfetcher-cert-requests");
WebDriver driver = new ChromeDriver(opts);
driver.get("http://www.google.com");
System.out.println("Title:" + driver.getTitle());
Try below code. It worked for me:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--ignore-urlfetcher-cert-requests");
webDriver = New ChromeDriver(chromeOptions);