While using Selenium WebDriver as web automation framework, I have a question -
how can I configure Google Chrome and Internet Explorer to save downloaded files to specific (not default) folder and also without showing additional "save file" windows.
Webdriver has a FirefoxProfile for Mozilla Firefox browser, but what about other browsers?
Example for Firefox :
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", "\\Somedir\");
profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip, application/x-gzip");
With Chrome, it can be done, it is just not as easy and straightforward as Firefox profile manipulation is.
Since the WebDriver bindings expose the ability to add in 'custom' abilities and command line parameters, you can give it any of the command line switches that Chrome knows about:
http://peter.sh/experiments/chromium-command-line-switches/
With this, we can give Chrome a profile to load, just as you do with Firefox.
However the WebDriver bindings don't expose the options as well as the Firefox profiles. So it has to be done manually.
Firstly, find out where your profiles live:
Go to this URL in Chrome.
chrome://version/
It will tell you what profile it's currently loading (and what command switches it is using). Copy the Profile Path into Explorer and go to it.
It should be, by default, using the Default profile directory. Go up on level in explorer, so you sit in the User Data folder.
Next step, create a new profile for Selenium to use. Open Chrome, go to Settings > Users > Add a new user. Give it a name.
Once created, Chrome will open up a new Chrome window for that user. Next step is to force Chrome, for this user only, to save downloads in a certain place.
This can be done either two ways. In the Chrome UI, go to the Settings, and change the download directory and ensure the checkbox next to it is unchecked, or to get a little more creative...
All Chrome preferences are stored in a file, stored in the users directory. You should have an explorer window open already, sitting at the User Data folder in Chrome's user folder. You should hopefully see it's created a new profile (probably called Profile 1). Go into it.
Now look for a file called Preferences (no extension).
Edit it with Notepad (it's a text document, with JSON).
Look for the download section, it will look like this:
"download": {
"directory_upgrade": true,
"extensions_to_open": ""
},
Add in this key, putting in the path you want to save your files to:
"default_directory": "PATH",
Make sure you escape any slashes in the path, with another backslash, in the same way it is escaped when you use the Visual Studio debugger.
For instance, C:\Bob\Jim\Downloads should be input as C:\\Bob\\Jim\\Downloads.
Save this file.
Now you have a profile where the downloads go to a certain place. How to make Chrome use this profile? You tell it which profile to open up at launch. The command line would be:
--profile-directory="Profile 1"
(Replace Profile 1 with whatever it is called in the User Data folder, if it isn't called that).
OK, we can tell Chrome to load up a particular profile, but how do we tell Selenium to do this too? Easy! Add it in as an 'additional command line switch'.
var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument(#"--profile-directory=""Profile 1""");
Selenium will take care on ensuring that command line switch is passed down to Chrome.
(Note: if anyone knows of an easier solution, let me know!).
As for IE, I have tested this on IE8 and IE9 and it works OK. IE can take into consideration a registry key for where the default downloads location is. I cannot comment on IE7 or below though.
Navigate to, using regedit to (it is a per Windows user settings):
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer
Add in a string value called Download Directory. It's value will be the exact location of where you want the downloads to go. No need to escape the path BTW.
Simply ensure the user you are logged into under Windows has set this value, and there will be no more setup needed.
Please try the below code for chrome. Even I'm looking for a similar option for IE.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
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();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOptions("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
Related
im looking example how can i start my normal google chrome in webdriver c#?
For now i use :
ChromeDriver driver;
public ChromeDriverService chromeDriverService;
chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("disable-infobars");
chromeOptions.AddExcludedArgument("enable-automation");
chromeOptions.AddAdditionalCapability("useAutomationExtension", false);
driver = new ChromeDriver(chromeDriverService,chromeOptions);
But it run my chromedriver.exe installed inside project. Can i just run my simple installed chrome? Without download any chromedriver.exe? It's important for me , because some website check if there is opened chromium with chromedriver.exe.
Is it possible to do that?
If you want not just open it but also perform some interaction with your site then you have to deal with Selenium and WebDriver.
You can try Python and undetected-chromedriver which has some workarounds preventing some systems to detect that your browser is running under webdriver control.
Unfortunately, it's the chromedriver.exe that lets you interact with Chrome so, without the driver, you wouldn't be able to interact with the actual browser.
If your issue is with chromium, try driving Mozilla with GeckoDriver
Selenium always uses chromedriver.exe for interacting with Chrome, I think you mean you want to load your default data directory. You can add a chrome option for this purpose:
chromeOptions.AddArguments(#"C:\Users\<your user name>\AppData\Local\Google\Chrome\User Data")
Take care, any other chrome must not be running that time with the same data directory. Like you may have run for your personal use.
You can also create a data directory somewhere else if you just want to show some realistic behavior to a website. But some websites are smart enough so it does not work every time.
I'm automating tests using Selenium and C# and I need to open an external app from the browser. The problem is, I always get this notification and it blocks the test execution.
Does anyone know how to deal with this?
Chrome stores the settings for the acceptance of protocol handlers in the user profile. When running Chrome from Selenium, Chrome doesn't seem to use the standard Chrome user profile by default, and instead uses some default settings that are not persisted.
To get around this, you can launch Chrome from the command line manually and manually specify a new --user-data-dir=c:\foo\bar profile location. (Point it to a new/empty directory and Chrome will populate it for you.)
Using this manually-launched browser, navigate to the page you need to interact with, activate the link, click the "always open" checkbox, and run the program once.
Next, close Chrome and save a copy of the entire new user profile directory. When you run your Selenium tests, make sure to always pass Chrome the same command line options pointing it to that user profile. These settings are now persisted, so the link will open without user intervention in the future. (This question may be of help to feed the right command line args to Chrome.)
For repeatable tests, you will probably want to save a static copy of this profile and redeploy it whenever you launch Selenium.
If you are using Javascript+Selenium or WebdriverJS then use this :
chromeOptions = {
'args': ['--test-type',
'--start-maximized',
'use-fake-ui-for-media-stream',],
'prefs': {
protocol_handler: {
excluded_schemes: {
'yourprotocolname': false
}
}
},
};
Since geckodriver v0.16.0 flashplayer is disabled by default.
Is there any possibility to start firefox with enabled flashplayer?
I'm using C#. My code right now:
var profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile("selenium"); //created firefox user named selenium
profile.SetPreference("plugin.state.flash", 1);
Code bellow doesn't work for me:
profile.SetPreference("dom.ipc.plugins.enabled.libflashplayer.so", true);
When I use this one:
profile.SetPreference("plugin.state.flash", 1);
firefox is asking if I want to enable flashplayer, and than refreshes page (with all inputs filled previously - so i got empty fields).
If I select "allow and remember", next time I start this code nothig is saved. I'm getting the same situation.
I found solution there:
How to enable Adobe Flash in FireFox Selenium webdriver with FirefoxProfile
If I replaced profile.setPreference("plugin.state.flash", 1); to profile.setPreference("plugin.state.flash", 2); it stopped to asking me if I want to enable flash player.
Here is the solution for you:
With Selenium 4.3.0, gecko driver v0.16.0 & Mozilla Firefox 53.0 this code works well with myfreecams.com.
It is worth mentioning that the default Firefox profile is not very automation friendly. When you want to run automation reliably on a Firefox browser it is advisable to make a separate profile. Automation profile should be light to load and have special proxy and other settings to run good test. You should be consistent with the profile you use on all development and test execution machines. If you used different profiles everywhere, the SSL certificates you accepted or the plug-ins you installed would be different and that would make the tests behave differently on the machines.
So, Create a New Firefox profile and use the same in the Test script involves three steps process. First you need to Start the Profile Manager, second is to Create a New Profile and third is to use the same profile in Test scripts. Let's assume we created a new FirefoxProfile by the name "debanjan".
Use the following code to open http://www.myfreecams.com/ with your new FirefoxProfile "debanjan":
String driverPath = "C:\\Utility\\BrowserDrivers\\";
//Mozila Firefox
System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
FirefoxDriver driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.navigate().to("http://www.myfreecams.com/");
PS: This code is in Java so you may have to convert it into C# format.
Let me know, if this helps you.
I use selenium FirefoxDriver in my tests and I run these tests parallel - in each thread is running separated Firefox instance. Everything works fine when I use normal FireFox, but if I want to run these tests with Firefox portable, the first instance launch successful, but second, third, etc... fails with this error:
Your Firefox profile cannot be loaded. It may be missing or inaccessible.
This is how I launch Firefox from code:
var profile = new FirefoxProfileManager().GetProfile("default");
var firefoxBinary = new FirefoxBinary("Path to FireFoxPortable.exe");
_driver = new FirefoxDriver(firefoxBinary, profile);
Any ideas what I am doing wrong?
Thanks.
The Firefox driver is trying to launch Firefox using a profile that is already in use. This is not possible as a profile can only be used once. The reason why it appears to be working while launching Firefox manually multiple times is because Firefox will reuse the existing running Firefox process with the already loaded profile.
Based on this information the solution to your problem is either 1) have the Firefox driver launch Firefox with unique/new profiles, 2) change your code so that only one instance of the Firefox driver is required.
To launch Firefox with multiple instances, use: firefox.exe -P "My Profile" -no-remote. Do not that the -no-remote parameter should not be used with the first profile launched, which in your case will be the "default" profile. More on this here: http://kb.mozillazine.org/Opening_a_new_instance_of_Firefox_with_another_profile.
To launch Firefox Portable with different profiles, if the previous commands are not applicable for Firefox Portable, follow the instructions here: http://portableapps.com/support/firefox_portable#second_profile.
I am using firefox webdriver. I want to store all the cookies and cache files into a custom directory. But its taking a temp directory instead of my directory . Here is my code:
FirefoxProfile firefoxProfile = new FirefoxProfile(path, false);
MessageBox.Show(firefoxProfile.ProfileDirectory); //Its showing blank
driver = new FirefoxDriver(firefoxProfile);
MessageBox.Show(firefoxProfile.ProfileDirectory); //Its showing the temp dir
//not my custom dir
How to make it take my directory and store all the cookies and files there ?
Thanks.
EDIT:
I need to make the selenium profile directory fixed (as it is changing all the time). So that I can use previous cookies and cache files. Can you give me any idea or any alternate way to accomplish it ?
You could just find the temp file path as you are already then copy your files into this folder with system commands after creating the driver. Then you would have access to them, though this is a little hacky but should work ( unless I've missed something crucial here, which I feel I may have :) )
FirefoxProfile.ProfileDirectory refers to a generated profile directory(obviously, available after browser instance creation only).
As per Selenium source code, profile directory is generated as a random directory in temp folder, hence could not be changed
// creates a random folder name in Path.GetTempPath()
this.profileDir = GenerateProfileDirectoryName();
and
public string ProfileDirectory
{
get
{
return this.profileDir;
}
}
Whereas the profileDirectory you specify during FirefoxProfile creation is used to load user.js.
Quote from mozillaZine:
A user.js file is an alternative method of modifying preferences,
recommended for advanced users only.
Upd.
The only way to solve your issue I could see, besides digging into selenium sources, is changing temp folder location before starting the webdriver to your desired folder and then changing it back to it's original value for current user.
Upd2.
Another possible solution(although not tried yet) is specifying ProfileDirectory from a previous webdriver run as profileDirectory for a new instance of FirefoxProfile for another instance of webdriver. Given that all the files from profileDirectory are copied to the generated temp folder, that could help you achieve desired functionality.
Is it absolutely necessary for you to use firefox? If you are using IE, won't this be taken care? From selenium jar help contents
-ensureCleanSession: If the browser does not have user profiles,make
sure every new session has no artifacts from previous sessions. For
example, enabling this option will cause all user cookies to be
archived before launching IE, and restored after IE is closed.
So, if you don't use that option while running tests in IE your cookies will stay. I haven't tried in webdriver, but I have seen the cookie being retained while using Selenium 1.