I'm using this code to add own extension, but before load the page it automaticly disabels. How can I enable it?
ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=...");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
To add an extension and disable the plugins with Chrome:
ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);
// add an extension
options.addExtensions(new File("C:\\extension.crx"));
// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[]{
"Adobe Flash Player", "Chrome PDF Viewer"});
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.co.uk");
Related
I am in the process of downloading a zip file and an XML file..and i want to save it in separate folde(which i want to create in the project folder itself..in eclipse)of my choice... Is there a way to do it using ChromeOptions ...also how to disable saving in default directory....
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", false);
prefs.put("download.prompt_for_download", true);
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("safebrowsing.enabled", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
options.addArguments("start-maximized");
options.addArguments("--safebrowsing-disable-download-protection");
options.addArguments("--safebrowsing-disable-extension-blacklist");
driver = new ChromeDriver(options);
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);
}
It opens in a new window with the default start menu and shows a notification that Chrome is being controlled by automated test software, but it does not go to the url.
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
What I also tried:
System.setProperty("webdriver.chrome.driver","C:\\Users\\"+System.getProperty("user.name")+"\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setBinary("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data\\");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.google.com");
The exception it gives
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed
(Driver info: chromedriver=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 61.65 seconds
I am using the lastest ChromeDriver 2.30 and Selenium 3.4.0 versions
Found the answer to my own question. It worked when I copied the Default folder from the profile path and moved it somewhere else.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/"+System.getProperty("user.name")+"/Desktop/");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
Try this:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "CHROME_DRIVER_PATH");
capabilities.setCapability("chrome.switches", Arrays.asList("--no-default-browser-check"));
HashMap<String, String> chromePreferences = new HashMap<String, String>();
chromePreferences.put("profile.password_manager_enabled", "false");
capabilities.setCapability("chrome.prefs", chromePreferences);
ChromeOptions options = new ChromeOptions();
options.setBinary("CHROME_BINARY_PATH");
options.addArguments("--test-type");
options.addArguments("--allow-running-insecure-content");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = ChromeDriver(capabilities);
If you don't want to move the profile, you can use the below (change %Profile% to the profile you want to use):
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("--profile-directory=%Profile%")
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com");
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.
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);