How to Set Default Download Directory in Selenium Chrome Capabilities

How to set default download directory in selenium Chrome Capabilities?

For Chromedriver try out with:

String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

Note:- Use File.separator to handle slashes, it will put syntax as per os it is executing the code.
In windows you need to use \\ for path while if you are using linux or mac then use //

Hope this helps. :)

Javascript : Chrome capabilities for "default download directory", "download.prompt_for_download", and "plugins.always_open_pdf_externally"

Based on this api-docs, I am able to set capabilities in Javascript except default download path. However, same can be achieved by await wdriver.setDownloadPath('C:\\Downloads');

var chrome = require("selenium-webdriver/chrome");

let downloadFilepath = "C:\\Downloads";
let options = await new chrome.Options();
options.addArguments("--disable-dev-shm-usage");
options.addArguments('--ignore-certificate-errors');
options.addArguments('--ignore-ssl-errors');
options.setUserPreferences({'download.default_directory': "C:\\Downloads"});
options.setUserPreferences({'download.prompt_for_download': false});
options.setUserPreferences({'download.directory_upgrade': true});
options.setUserPreferences({'plugins.always_open_pdf_externally': true});

driver = await chrome.Driver.createSession(options);

Let me know if "download.default_directory" can be set using capabilities instead of wdriver.setDownloadPath.

How to download files in customized location using Selenium ChromeDriver and Chrome

To download the required file within Automation Demo Site to a specific folder using Selenium, ChromeDriver and google-chrome you need to pass the preference "download.default_directory" along with the value (location of the directory) through add_experimental_option() and you can use the following solution:

Code Block:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Data_Files\output_files"
})
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("http://demo.automationtesting.in/FileDownload.html")
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "textbox"))).send_keys("testing")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "createTxt"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "link-to-download"))).click()

Snapshot:

chrome_download

Setting download directory in webdriver using JS

According to this issue, chromeOptions key doesn't work as intended. Use goog:chromeOptions to set options for chrome capabilities:

const chromeCapabilities = webdriver.Capabilities.chrome();

chromeCapabilities.set('goog:chromeOptions', {
'args': ['disable-infobars'],
'prefs': {
'download': {
'default_directory': '/home/{user}/Downloads/Chrome_test',
'prompt_for_download': 'false'
}
}
});

const driver = new webdriver.Builder()
.withCapabilities(chromeCapabilities)
.build();

Changing default download location in Chrome while working with Hybrid framework using POM pattern in Selenium

You are on the right path. you just have to pass the options in baseclass driver instance creation line.

// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();

// Use File.separator as it will work on any OS
prefs.put("download.default_directory", "C:\\Users\\pd\\Desktop\\AHNPTest");


// Adding cpabilities to ChromeOptions
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);


// Launching browser with desired capabilities
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options); //<====== This is the line

How to change download path during runtime using Selenium with chrome driver

Suppose you want to specify download folder for each test method.

  1. Add parameter for downloadPath in initialization in TestBaseClass.
  2. Add parameter for downloadPath in setup in ANA_TC16_RiskAnalysisNewTest, remove the @BerforMethod annotation and update each test method to call setup in begin with desired downloadPath.
public class TestBaseClass {
public static void initialization(String downloadPath) throws InvocationTargetException {
try {

// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();

// Use File.separator as it will work on any OS
prefs.put("download.default_directory", downloadPath);
...
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;

// @BeforeMethod
public void setUp(String downloadPath) {
try {
initialization(downloadPath);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}

@Test
public void chapterrLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\ChapterLevel");
New.hoverTest();
...
}

@Test
public void practiceLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\PracticeLevel");
New.hoverTest();
...
}
...


Related Topics



Leave a reply



Submit