Selenium Firefox Preference Changed but not Applied - c#

I am using Selenium and Firefox for automated testing, and I need the files to download automatically. Here are two links that I've used to setup my code.
Auto download PDF in Firefox
Set Firefox profile to download files automatically using Selenium and Java
To summarize the articles, the code should look like this:
FirefoxOptions options = new FirefoxOptions();
options.setPreference("browser.download.folderList", 2);
options.setPreference("browser.download.dir", "C:\\Windows\\temp");
options.setPreference("browser.download.useDownloadDir", true);
options.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
WebDriver driver = new FirefoxDriver(options);
When I run my test, the auto-download fails. I checked in the about:config and the settings have been changed as intended by the code.
(about:config screenshot)
Also, within that driver instance, if I change any setting and then reapply the same setting, the auto-download works. Is there a setting or step with the webdriver that I'm missing that then applies the new settings?
Here are the Selenium, Firefox, and GeckoDriver versions I've tested with:
Selenium: v3.12.0
Firefox: 59.0.3, 60.0.1
GeckoDriver: v0.19.0-win64, v0.20.0-win64, v0.21.0-win64

As far as i know is pretty difficult download files with selenium because the browser open some dialogs that is not possible control from javascript. Watch this link, I hope will be useful

Related

Is there a way to activate IE mode in Edge Options?

Hello,
I want to achieve this by having an option inside the EdgeDriver but I cant seem to find it anywhere on the map?
I am trying to open a page in IE mode inside Edge with Selenium and EdgeDriver.
Is there a way to achieve this great thing? [pun intented]
I can see 2 questions in this thread.
Is there a way to activate IE mode in Edge Options?
There is no way to activate IE mode bypassing the Edge options parameter in the Selenium Edge driver.
I am trying to open a page in IE mode inside Edge with Selenium and EdgeDriver. Is there a way to achieve this great thing?
Yes, it is possible to automate the IE mode in the new MS Edge browser using the Selenium web driver.
The new Microsoft Edge allows you to run IE11 validation for legacy sites in addition to your modern experiences. To run your IE11 tests in Microsoft Edge, download the IEDriverServer from Selenium. Then you must pass in a capability to put Microsoft Edge into IE Mode and then run your tests.
Because this capability puts the whole browser into IE11 Mode, you cannot simultaneously test content that should render in the modern Chromium engine, but you should be able to run all of your IE11 tests and validate the rendering in Microsoft Edge. Note that this code requires an update to IEDriverServer which should be included in the next release of Selenium.
After you download the new IEDriverServer from SeleniumHQ and follow the directions for the “Required Configuration” as documented here, you can run the following code to launch the new Microsoft Edge in IE11 mode and run some tests:
static void Main(string[] args)
{
var dir = "{FULL_PATH_TO_IEDRIVERSERVER}";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieService = InternetExplorerDriverService.CreateDefaultService(dir, driver);
var ieOptions = new InternetExplorerOptions{};
ieOptions.AddAdditionalCapability("ie.edgechromium", true);
ieOptions.AddAdditionalCapability("ie.edgepath", #"\\msedge.exe");
var webdriver = new InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(30));
webdriver.Url = "http://www.example.com";
}
Output:
Notes:
Make sure you are using the latest version of the IE driver server.
I suggest making a test with the latest version of the Stable Edge browser.
Try to pass the full path of the Edge browser in the 'ie.edgepath' capability. For example:
ieOptions.AddAdditionalCapability("ie.edgepath", #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe");
Make sure you close all the already opened instances and tabs of the Edge browser before running the code. Otherwise, it will generate an error.
References:
Scroll to the Automating Internet Explorer mode point in this link.
kypflug/webdriver-edge-ie-mode.cs
Below code (which is in VB.NET, but you can easily modify it to C#) will start Chromium Edge in IE Mode
Dim ieService = InternetExplorerDriverService.CreateDefaultService("DIRECTORY_PATH_HAVING_IEDriverServer.exe", "IEDriverServer.exe")
Dim ieOptions = New InternetExplorerOptions
ieOptions.IgnoreZoomLevel = True
ieOptions.AddAdditionalCapability("ie.edgechromium", True)
ieOptions.AddAdditionalCapability("ie.edgepath", "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
Dim driver = New InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(60))
driver.Navigate().GoToUrl("https://example.com")
You can download IEDriverServer from https://www.selenium.dev/downloads/

How to open the Default Chrome Profile through Selenium, ChromeDriver and GoogleChrome

I want to load a new Selenium ChromeDriver that is using Chrome as if I open Chrome from my dock (Essentially it'll have all my extensions, history, etc.)
When I use the following code:
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
It loads the Chrome browser with me signed into my Gmail and with all my extensions, just like I want, but the rest of my code:
chromeDriver.Navigate().GoToUrl("https://www.youtube.com/");
doesn't execute. But when I use the following
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\Andrea\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
The rest of my code executes perfectly (Notice the 'Default' added to the end of the first Argument). Any tips or suggestions on how I can get the first block of code (The one without 'Default' on the end) to execute the rest of my program would be great. Thanks!
I know this is an old question, but what worked for me is to do remove the "C:\" and replace all of the backslashes with forward slashes. So, with that from the original question, this should work to load the default profile:
options.AddArgument("user-data-dir=/Users/User/AppData/Local/Google/Chrome/User Data");
The Default Chrome Profile which you use for your regular tasks may contain either/all of the following items:
History
Bookmarks
Cookies
Extensions
Themes
Customized Fonts
All these configurations untill and unless are part of your Test Specification it would be a overkill to load them into the session initiated by Selenium WebDriver. Hence it will be a better approach if you create a dedicated New Chrome Profile for your tests and configure it with all the required configuration.
Here you will find a detailed discussion on How to create and open a Chrome Profile
Once you have created the dedicated New Chrome Profile for your tests you can easily invoke the Chrome Profile as follows:
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-data-dir=C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2");
options.AddArgument("disable-infobars");
options.AddArgument("--start-maximized");
ChromeDriver chromeDriver = new ChromeDriver(options);
chromeDriver.Navigate().GoToUrl("https://www.youtube.com/");
Here you will find a detailed discussion on How to open URL through default Chrome profile using Python Selenium Webdriver
I have the same issue. I don't know how to fix it, I guess the root cause is white space in profile path.
I know a workaround for this. Just copy the C:\\Users\\Andrea\\AppData\\Local\\Google\\Chrome\\User Data to c:\myUserData (no space in the path).
Then add the argument.
options.AddArgument("user-data-dir=C:\\myUserData");
This is an Old question, but if you are facing this issue, all you have to do is close all tabs, Just shut down the chrome window..
Selenium can't use the data since it is already in use.
Hope you fond this helpful.

Seleniums Gecko driver crashes when going to new web pages

I am using the new Gecko Driver to test in Firefox. When I change the url it often fails. I have a test that goes to 10 different pages to perform a very basic health check. The pages always load when using Firefox manually (not Gecko/Selenium), but when using the Gecko driver it fails on the GoToUrl().
It's not consistent which of the ten pages it fails on, but it is always consistently failing on one of the ten. When it fails Firefox closes and an error message pops up asking if I want to submit the details to Mozilla.
Am I doing something wrong, or is there some restriction with the Gecko driver that I am unaware of? The Chrome and internet explorer drivers handle the exact same test just fine.
var service = FirefoxDriverService.CreateDefaultService(DriverPathOnSystem);
var driver = new FirefoxDriver(service);
driver.Navigate().GoToUrl(url);
EDIT
Selenium.Support v3.4.0 (nuget)
Selenium.WebDriver v3.4.0 (nuget)
Firefox 51.0.1 (32-bit) but also using the most recent version on the
build machine and it has the same failure there
Gecko 0.16.0
Upon further research I discovered I was actually using WebDriver v3.2.0.
I updated WebDriver to v3.4.0,
updated FireFox to 53.0.0,
updated GeckoDriver to 0.16.1
cleaned the solution and rebuilt and it worked.

Disable security features in Firefox

I am testing a website using selenium web driver. The particular site contains nested iframes. I cannot access content of Iframes, because of the Same Origin Policy. Therefore I disabled web security in chrome web driver and access those contents using following Jquery script.
$(‘#data’).find(‘iframe’).contents().find(‘html’)
I achieved this in chrome by setting these features when it is initializing.
ChromeOptions options = new ChromeOptions();
options.AddArguments("--allow-file-access-from-files");
options.AddArguments("--disable-web-security");
IWebDriver driver = new ChromeDriver(options);
(Domain of iframes are differ with my hosted domain.)
Now I need to do this in Firefox. I followed instructions in this URL. But that is not working.
about:config -> security.fileuri.strict_origin_policy -> false
Then I tried with these edits
about:config -> security.mixed_content.block_active_content -> false
about:config -> security.mixed_content.block_display_content -> false
Then I loaded Firefox profile and init web driver using that.
FirefoxProfile profile = new FirefoxProfile(#"C:\Users\admin\AppData\Roaming\Mozilla\Firefox\Profiles\0nan0gbv.default");
IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver(profile);
I haven't got what I expected using above edits. Error was Permission denied to access property 'document'
Then I tried set these settings into new Firefox profile(not use default one in my machine) and load it.
OpenQA.Selenium.Firefox.FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.privatebrowsing.autostart", true);
profile.SetPreference("security.fileuri.strict_origin_policy", false);
profile.SetPreference("security.mixed_content.block_active_content", false);
profile.SetPreference("security.mixed_content.block_display_content", true);
IWebDriver driver = new OpenQA.Selenium.Firefox.FirefoxDriver(profile);
Then I got this error and couldn't able to find proper solution.
Preference security.fileuri.strict_origin_policy may not be overridden: frozen value=False, requested value=False
I found this question has many ways to overcome SOP. But those are not answers for my problem.
There should be way to achieve this. I am using Firefox 29.0.1

Selenium WebDriver Disabled my Firefox addons

I use Selenium WebDriver in my C# winforms application. When I run application and open Firefox, my addons are disabled. How to leave the addons enabled?
You can explicitly set the desired addons to ON. For example, if you want to enable firebug, find out the location of the zip or xpi of the addon and then use the following code:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.AddExtension("C:\\firebug.xpi");
ffprofile.SetPreference("extensions.firebug.currentVersion", "1.11.4");
IWebDriver driver = new FirefoxDriver(ffprofile);
The only thing I did to resolve is just by adding following to the pom.xml and it worked fine for me. Currently I'm running tests on Firefox 43.0.3 with Selenium 2.48.2. and also by have a dependency for the Firefox browser in the pom.xml.
org.seleniumhq.selenium selenium-firefox-driver 2.48.2
org.seleniumhq.selenium selenium-java 2.48.2
After using this if you fail to use your add on than drop message I will provide you some more options but hope fully you will get result from this.

Categories