How to download an excel spreadsheet without opening a windows popup window - java

There is a spreadsheet to download using java+selenium in headless mode.
The thing is I'm using to execute this test cases a Linux server(CI/CD). The code is working 100% correct, When I'm using my local computer(Windows) to execute this code.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-infobars");
chromeOptions.addArguments("--browser.helperApps.neverAsk.saveToDisk",
"application/vnd.ms-excel,
vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/excel");
chromeOptions.addArguments("--test-type");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--no-zygote");
chromeOptions.addArguments("--disable-dev-shm-usage");
Map<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.prompt_for_download", "false");
chromePrefs.put("cmd", "Page.setDownloadBehavior");
if (OS.startsWith("windows")) {
chromePrefs.put("download.default_directory", path+WIN_RESOURCES_PATH);
System.setProperty("webdriver.chrome.driver", ".\\src\\main\\resources\\driver\\chromedriver.exe");
if (Parameters.instance().getHeadless().toLowerCase().equals("true")) {
chromeOptions.addArguments("--headless");
}
} else if (OS.startsWith("linux")) {
chromePrefs.put("download.default_directory", path+LIN_RESOURCES_PATH);
System.setProperty("webdriver.chrome.driver", Parameters.instance().getChromeDriver());
if (Parameters.instance().getHeadless().toLowerCase().equals("true")) {
chromeOptions.addArguments("--headless");
}
}
chromeOptions.setExperimentalOption("prefs", chromePrefs);
chromeOptions.addArguments("window-size=1920,1080");
driver = new ChromeDriver(chromeOptions);
driver.get(getUrl());
driver.findElement(locator).click;
driver.switchTo().alert().accept();
My code is something like this. When Selenium test clicks the download button, opening another tab in browser and also a popup window, asking where to "Save" the file.
"I'm running these tests in headless chrome"

For me the following code works for pdf download (Windows)
With these option no promot and it will download your predefined path.
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
Path download_folder = Paths.get(System.getProperty("user.home") + "/Downloads", new String[0]);
HashMap<String, Object> prefs = new HashMap();
prefs.put("download.default_directory", download_folder.toAbsolutePath());
prefs.put("download.prompt_for_download", Boolean.valueOf(false));
prefs.put("download.directory_upgrade", Boolean.valueOf(true));
prefs.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
HashMap<String, Object> options = new HashMap();
options.put("prefs", prefs);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chromeOptions", options);
driver = new ChromeDriver(capabilities);

Related

Chromedriver is not downloading the pdf file on clicking the link

Using ChromeDriver version 80.0.3987.106 and the Chrome Capabilities Below.
I want to download a PDF to my local default Downloads location so that the test code can read it for assertions. However, the file is opening in a new Tab in the browser.
public DesiredCapabilities getDesiredCapabilities(Proxy proxySettings) {
TestContext.get().setBrowser("Chrome", "latest");
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("safebrowsing.enabled", "true");
chromePrefs.put("download.default_directory","C:\\Users\\varghesevineeth\\Desktop\\Vineeth" );
chromePrefs.put("profile.default_content_setting_values.notifications", 2);
chromePrefs.put("plugins.plugins_disabled", new String[] { "Adobe Flash Player", "Chrome PDF Viewer" });
chromePrefs.put("plugins.always_open_pdf_externally", true);
chromePrefs.put("pdfjs.disabled", true);
chromePrefs.put("profile.default_content_setting_values.automatic_downloads", 1);
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("test-type");
options.addArguments("--ignore-certifcate-errors");
options.addArguments("--disable-extensions");
options.addArguments("start-maximized", "disable-popup-blocking");
options.addArguments("disable-infobars");
options.addArguments("--disable-application-cache");
DesiredCapabilities chromeCapabilities = DesiredCapabilities.chrome();
chromeCapabilities.setCapability("name", MDC.get("testname"));
chromeCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
chromeCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
chromeCapabilities.setCapability("download.prompt_for_download", false);
return addProxySettings(chromeCapabilities, proxySettings);
}

WebDriver save problems in headless mode

I'm using Java+ChromeDriver on Mac for my project.
Here is some code:
System.setProperty("webdriver.chrome.driver", TestConfig.driverURL());
ChromeOptions options = new ChromeOptions();
options.addArguments("headless").addArguments("window-size=2560x1440");
String downloadFilePath = "some file path";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadFilePath);
options.setExperimentalOption("prefs", chromePrefs);
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
When I comment this line:
options.addArguments("headless").addArguments("window-size=2560x1440");
all is working fine and I download a couple of files, but Chrome doesn't work in headless mode.
When this line is not commented files don't download.
Can anybody tell my how I can download files using ChromeDriwer in headless mode?
Thanks.
Here is what worked for me:
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
prefs = {'download.default_directory' : out_path}
options.add_experimental_option('prefs', prefs)
download_path = './output/'
driver = webdriver.Chrome('./chromedriver', chrome_options=options)
driver.command_executor._commands["send_command"] = ("POST",
'/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow',
'downloadPath': download_path}}
command_result = driver.execute("send_command", params)

Chrome keeps opening the pdf file in a new tab

I try to download a file directly from a link but Chrome keeps opening the pdf file in a new tab.
Here is the code I gathered from all issues I found :
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
String downloadFilepath = "C:\\Users\\i016800\\Downloads";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
chromePrefs.put("download.directory_upgrade", "true");
chromePrefs.put("download.extensions_to_open", "");
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("pdfjs.disabled", true);
chromePrefs.put("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
chromePrefs.put("plugins.plugins_disabled", new String[]{ // disable flash and the PDF viewer
"Adobe Flash Player", "Chrome PDF Viewer"});
//Save Chrome Options
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> plugin = new HashMap<String, Object>();
plugin.put("enabled", true);
plugin.put("name", "Chrome PDF Viewer");
chromePrefs.put("plugins.plugins_list", Arrays.asList(plugin));
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
options.addArguments("--always-authorize-plugins=true");
options.addArguments("--disable-extensions");
options.addArguments("start-maximized"); // Open Chrome in Full Screen
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
cap.setPlatform(org.openqa.selenium.Platform.ANY);
cap.setCapability("prefs.download.directory_upgrade", true);
WebDriver driver = new ChromeDriver(options);
String adresseJarvis = "http://intranet.renault.com/declic-com/post/116235/2017/06/renault-assemblee-generale-2017/";
driver.get(adresseJarvis);
driver.findElement(By.xpath("//html/body/div[1]/div[6]/div/div/div/div/div[2]/div[3]/div/div[1]/div/ul/li/a")).click();
I know some options are correctly loaded because when I activate options.addArguments("start-maximized"), Chrome begins in Full Screen.
I also tried to change chrome settings manualy before execution but it doesn't work neither.
My config :
Chrome Driver 2.29
Java 1.7.0-60-b19
Eclipse Indigo build 20120216-1857
Os Windows 7 Entreprise
Chrome 58.0.3029.110 (64-bit)
Selenium 2.47
A colleague of mine finnaly managed to solve my problem.
Here is the code :
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String adresseJarvis = "http://intranet.renault.com/declic-com/post/116235/2017/06/renault-assemblee-generale-2017/";
driver.get(adresseJarvis);
WebElement printLink=driver.findElements(By.xpath("//html/body/div[1]/div[6]/div/div/div/div/div[2]/div[3]/div/div[1]/div/ul/li/a")).get(0);
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",printLink,"download","");
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])",printLink,"target","_blank");
driver.findElement(By.xpath("//html/body/div[1]/div[6]/div/div/div/div/div[2]/div[3]/div/div[1]/div/ul/li/a")).click();
I hope it will help.

How to translate webpage language using Selenium ChromeDriver

When I run the scripts, webpage opened in Turkey language. I want to convert in English language,tried below piece of code using chrome options but it is setting browser language to English not webpage language.please help
static DesiredCapabilities setChromeCapabilities()
{
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 1);
prefs.put("download.default_directory", "C:\\dev\\tmpReport");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("chrome.switches","--disable-extensions");
options.addArguments("--start-maximized");
options.addArguments("--lang=en"); **//tried adding this line of code**
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
return capabilities;
}

Selenium multiple desired capabilities settings

I am trying to set multiple desired capability,for an instance of a chrome driver object in selenium.
I would like the browser to first
set the download location of files, then to disable the pdf viewer plugin in Chrome browser. Can anyone assist?
Code snippet for disabling PDF viewer plugin:
DesiredCapabilities caps = DesiredCapabilities.chrome();
Map<String, Object> preferences = new HashMap<String, Object>();
preferences.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
ChromeOptions options1 = new ChromeOptions();
options1.setExperimentalOption("prefs", preferences);
caps.setCapability(ChromeOptions.CAPABILITY, options1);
Code snippet for setting the download location:
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "C:\\Users\\user\\Downloads\\");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);
Don't create two separate ChromeOptions just because you want multiple capabilities. That's why they take maps. Just put both key value pairs in a single map and then add that to the options object ... eg:
DesiredCapabilities caps = DesiredCapabilities.chrome();
Map<String, Object> preferences = new HashMap<>();
preferences.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
preferences.put("download.default_directory", "C:\\Users\\user\\Downloads\\");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", preferences);
caps.setCapability(ChromeOptions.CAPABILITY, options);

Categories

Resources