Disable chrome download multiple files confirmation - google-chrome

I developed a crawler with ruby watir-webdriver that downloads some files from a page. My problem is that when I click to download the second file, Chrome opens a bar in the top asking for confirmation that I am downloading multiple files from this website.
Once this is used by webdriver, I cannot confirm the download. Is there anyway to avoid this confirmation? I am thinking if is there any configuration to avoid it or if is there an extension to do this or even if I can click on the confirmation with webdriver.
thanks

I'm using Chrome 49 and none of the other solutions worked for me.
After some research I found a working solution:
ChromeDriver createChromeDriverWithDownloadFolder(String folder) {
Map<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", folder);
chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(cap);
}
It seems as if these settings are constantly changing. Therefore, here's how I found the right solution for my setup:
Open Chrome and go to chrome://version/ to find the path of your profile
In Default/Preferences is a json file called Preferences. Open it and search for automatic_downloads.
In my case the interesting part of the file looked like this:
..."profile": {
"avatar_bubble_tutorial_shown": 1,
"avatar_index": 0,
"content_settings": {
"clear_on_exit_migrated": true,
"exceptions": {
"app_banner": {},
"auto_select_certificate": {},
"automatic_downloads": {
"[.]localhost:63342,": {
"setting": 1
},...
From that I could derive that the right setting would be chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );

As of Chrome 56.0.2924.87, February 17, 2017, the only preference you need to set (however you set them for your webdriver) is:
'profile.default_content_setting_values.automatic_downloads': 1
Giving an updated answer because most answers here use outdated preferences or show other preferences that are unnecessary.

for new chrome (version 46 or newer) this options was changed
now your hash must looks like this:
prefs = {
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version olde ~42
'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome newer 46
}
}
browser = Watir::Browser.new :chrome, options: {prefs: prefs, args: ['--test-type', '--disable-infobars'}

Here is the solution for Java - Selenium implementation
We faced hard time fixing this, as we wanted to add automation test for functionality which downloads set of PDFs on a single download link.
Map<String, Object> prefs = new HashMap<String, Object>();
//To Turns off multiple download warning
prefs.put("profile.default_content_settings.popups", 0);
prefs.put( "profile.content_settings.pattern_pairs.*.multiple-automatic-downloads", 1 );
//Turns off download prompt
prefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOptions("prefs", prefs);
driver = new ChromeDriver(options);
Hope this help to someone.

It seems that the solution is different for older and newer chromedriver versions and that is adding to the confusion.
chromedriver
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory
b = Watir::Browser.new :chrome, :profile => profile
chromedriver2
prefs = {
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1},
}
}
b = Watir::Browser.new :chrome, :prefs => prefs
Today most people are probably using chromedriver2 version and that is a solution that should work fine. It worked ok in my watir scripts as I am not getting the message: "This site is attempting to download multiple files. Do you want to allow this?" anymore.

Java solution:
cap = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
Map<String, Object> content_setting = new HashMap <>();
content_setting.put("multiple-automatic-downloads",1);
prefs.put("download.prompt_for_download", "false");
prefs.put("profile.default_content_settings", content_setting);
options.setExperimentalOption("prefs", prefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);

this is what worked for me:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("profile.default_content_setting_values.automatic_downloads", 1);
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);

This bug/enhancement has been raised in the chromedriver page at the below URL:
http://code.google.com/p/chromedriver/issues/detail?id=130
Bug/Enhancement Status: Yet to be resolved.

I have tried to do it on page load client-side using markups.
<META HTTP-EQUIV="Content-Disposition" CONTENT="inline" />
It seems to work (it is working at this moment, in overriding).
But time will tell (might not have effect on future CHROME's, you know what I mean).
There are a list of available header fields published on a couple of sites which I find extremely helpful. Hope it will help you, as well.
https://www.w3.org/Protocols/HTTP/Issues/content-disposition.txt
https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-2

Related

How to disable chrome pdf viewer in new tab with java in playwright

I'm trying to disable the pdf viewer in a new tab when I use chromium in Playwright. Do you know how can achieve that? Firefox has a method, it's called .setFirefoxUserPrefs().
For chrome, it's not implemented, but found the following thing:
chrome://settings/content/pdfDocuments +++ accessible by playwright.
How it can be implemented?
I tried to take know-how from selenium, but for the playwright, it just doesn't work.
I tried:
how to disable chrome pdf viewer in selenium and it should auto download in the default downloads when any pdf occurs
Disabling PDF Viewer plugin in chromedriver
but without any success, can you support me with that?
Including, I tried that:
private void PlaywrightSmokeTestConfigureInit(String browserType) {
playwright = Playwright.create();
ArrayList<String> arguments = new ArrayList<>();
arguments.add("--start-fullscreen");
boolean headless = false;
Map<String, Object> map = new HashMap<String, Object>();
map.put("pdfjs.disabled", true);
map.put("pdfjs.enabledCache.state", false);
map.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
map.put("plugins.always_open_pdf_externally", true);
map.put("profile.default_content_settings.popups", 0);
map.put("safebrowsing.enabled", "true");
switch (browserType) {
case "chromium":
browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel(browserType).setHeadless(headless).setArgs(arguments)
.setFirefoxUserPrefs(map));
break;
case "firefox":
browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setChannel(browserType).setHeadless(headless).setArgs(arguments));
break; ...

How to enable verbose logging in ChromeDriver? (GebConfig)

I have to enable verbose logging in Chrome / ChromeDriver to see why my geb tests are failing. Does anybody know how I can do that. Heres my GebConfig:
String chromeDriverDownloadFullPath = "https://chromedriver.storage.googleapis.com/${chromeDriverVersion}/${chromeDriverZipFileName}"
File chromeDriverLocalFile = downloadDriver(
currentPlatformName,
chromeDriverDownloadFullPath,
chromeDriverExecFileName,
'zip',
"chrome",
chromeDriverVersion)
System.setProperty('webdriver.chrome.driver', chromeDriverLocalFile.absolutePath)
Locale locale = getLocale()
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=" + locale.country);
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--user-data-dir=/data");
DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome()
Map<String, Object> prefs = new HashMap<>()
prefs.put("intl.accept_languages", locale.toLanguageTag())
options.setExperimentalOption("prefs", prefs)
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options)
ChromeDriver chromeDriver = new ChromeDriver(options)
chromeDriver.manage().window().setSize(getDimension())
return chromeDriver
To obtain verbose logs from ChromeDriver we can configure the logfile and the type_of_logging as follows:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
System.setProperty("webdriver.chrome.logfile", "C:\\Utility\\BrowserDrivers\\chromedriver.log");
System.setProperty("webdriver.chrome.verboseLogging", "true");
Update :
I can see from your code you have provided :
System.setProperty('webdriver.chrome.driver', chromeDriverLocalFile.absolutePath)
In a similar fashion try to provide :
System.setProperty('webdriver.chrome.logfile', chromeDriverLocalFile.absolutePath);
System.setProperty('webdriver.chrome.verboseLogging', boolean);
From the documentation of ChromeDriver - WebDriver for Chrome
System.setProperty("webdriver.chrome.logfile", "Your path");
System.setProperty("webdriver.chrome.verboseLogging", "true");

ChromeDriver 2.29 Unable to set automatic downloads to Allow by default

After upgrading to ChromeDriver 2.29, the default value for 'Automatic Downloads 'localhost:9000' is set to 'Ask'. Whenever my tests clicks on a link which invokes a download, the Save As windows dialog opens up. Previously it would download silently to the default Downloads folder of Chrome.
How do I change this setting's default value to 'Allow' in chromedriver (not chrome)?
I've tried using the chrome.switches but they didn't work:
chrome.switches=--disable-extensions,--disable-infobars,--allow-insecure-localhost,--safebrowsing-disable-download-protection
The default setting in Chrome is 'Allow' for all sites.
'http://localhost:9000' has also been added to Exceptions list.
You can set default download location using capabilities. It will download the file to that folder, it won't give any pop up's
Just try with below line of code
DesiredCapabilities capabilities = new DesiredCapabilities();
String downloadPath = System.getProperty("user.dir")+ "\\Downloads";
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", downloadPath);
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptionsMap);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
Let me know if you face any issue

How do you automatically open the Chrome Devtools tab within Selenium (C#)?

I see that there's a relatively new option to open Chrome with Devtools open from the command line, which I have gotten to work from my Windows 8.1 command line using a call like this:
c:\Program Files (x86)\Google\Chrome\Application>"chrome.exe" --auto-open-devtools-for-tabs
When I try to add this option on the same box when creating my ChromeDriver in Selenium (in C#), however, the option seems to be ignored.
var options = new ChromeOptions();
options.AddArgument("auto-open-devtools-for-tabs");
string executingAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
string driverPath = Path.Combine(Path.GetDirectoryName(executingAssembly), "ChromeWebDriver");
_driver = new ChromeDriver(driverPath, options);
I've tried a few variations on theme to make sure options are working at all, including...
var options = new ChromeOptions();
options.AddArguments(new[] { "start-maximized", "auto-open-devtools-for-tabs"});
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("auto-open-devtools-for-tabs");
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddExcludedArgument("auto-open-devtools-for-tabs");
... as well as setting those with -- in front of each option string. All I get from any of those are maximized windows.
I get the feeling the auto-open-devtools-for-tabs argument's not supported by Selenium's Chrome Web Driver, but I'm not sure why that wouldn't support the same set of options as the "full" app.
Anyone have this option working with Selenium in C#, or know why it shouldn't be working in this case?
This is not unlike this question, but here I'm asking specifically about the auto-open-devtools-for-tabs option with C#. That asker claims not to have had any luck with options, and was asking how to open devtools from "within" Selenium, looking for a method explicitly before this option existed.
I've tried this with in VS 2017, Selenium v3.12.1#, Firefox v60.0.2, Chrome v66, Nunit v3.10.1, Gecko Driver v20.1, and Chrome driver v2.4 (all using C#).
I tried to search for Firefox but did not have any success. I did find a solution for Chrome v66.
Please provide profile like this: options.AddArguments("--auto-open-devtools-for-tabs");
This is a complete chrome driver implementation:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");
options.AddArguments("disable-infobars");
options.AddArguments("--disable-notifications");
options.AddArguments("--auto-open-devtools-for-tabs");
driver = new ChromeDriver(DrivePath, options, TimeSpan.FromSeconds(100));
See also this post: "List of Chromium Command Line Switches"
Below commands are NOT working, this is issue with Geckodriver so Gecko team has to provide some solution or fix for that:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.F12);
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Perform();
Actions action = new Actions(driver); action .KeyDown(Keys.Control)
.SendKeys(Keys.F12).KeyUp(Keys.Control).Perform();
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Click();
Following the thread on SO-12212504 and leading from the selected answer.
One of the solution to this would be pressing F-12 [Key F12 Documentation] key using :
// without an element
new Actions(driver).SendKeys(Keys.F12).Perform();
// send keys to body
new Actions(driver).SendKeys(driver.FindElement(By.XPath("//body")), Keys.F12).Perform();
On the other side could you try and use AddUserProfilePreference from amongst the ChromeOptions Methods :
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("auto-open-devtools-for-tabs", "true");
Note : I am not very sure about the parameter name, but I hope you can find something corresponding here.
Edit : Some more attempts using keyboard shortcuts for the same -
Windows : [F12 or Ctrl + Shift + I]
String openDevTools = Keys.chord(Keys.CONTROL, Keys.SHIFT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Mac : [Cmd + Opt + I]
String openDevTools = Keys.chord(Keys.COMMAND, Keys.ALT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Ruby: must have installed latest selenium-webdriver (3.7.0) gem
options1 = Selenium::WebDriver::Chrome::Options.new
options1.add_argument('--auto-open-devtools-for-tabs')
driver = Selenium::WebDriver.for :chrome, options: options1
driver.get("https://stackoverflow.com")
I think the issue is with your options being declared as a var and not ChromeOptions, this code open google.com with dev tools open
public static void Scraps()
{
//Declare options variable and set dev tools argument
ChromeOptions co = new ChromeOptions();
co.AddArguments("--auto-open-devtools-for-tabs");
//Initiate driver instance and go to google.com
IWebDriver driver = new ChromeDriver(co);
driver.Url = "https://www.google.com/";
}

Appium - running browser tests without clearing browser data

I'm testing a web application on Chrome, Android (real device, not emulator) using Appium. Whenever I launch a test, all browser data (bookmarks, history etc.) is deleted. Is there any way to stop this from happening?
I tried setting the noReset capability to true, but that didn't help.
Thank you in advance for any help
public static Uri testServerAddress = new Uri("http://127.0.01:4723/wd/hub"); // Appium is running locally
public static TimeSpan INIT_TIMEOUT_SEC = TimeSpan.FromSeconds(180);
public void SetUpTest()
{
if (driver == null)
{
DesiredCapabilities testCapabilities = new DesiredCapabilities();
testCapabilities.SetCapability("browserName", "Chrome");
testCapabilities.SetCapability("platformName", "Android");
testCapabilities.SetCapability("deviceName", "S(Galaxy S5)");
testCapabilities.SetCapability("noReset", true);
AppUrl = "http://www.google.com/"; //for example
driver = new RemoteWebDriver(testServerAddress, testCapabilities, INIT_TIMEOUT_SEC);
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, globalTimeoutInSec));
driver.Navigate().GoToUrl(AppUrl);
}
}
Chromedriver always starts totally fresh, nothing is keeping.
There is option to re-use the existent one (using desired capability androidUseRunningApp) but unfortunately Appium any way will kill it.
Please see more details in this post