how to use a FirefoxProfile with a IWebDriver - c#

I have lots of problem with Firefox. For most of them, I found lots of solution that use FirefoxDriver object. However I need to use RemoteWebDriver.
All the solutions to my problem use a FirefoxProfile object. is there a way to use this profile for the IWebDriver?
One of the thing I need to do is use the profile to do this:
Firefox webdriver opens first run page all the time
here is how I use my IWebDriver:
driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Firefox(), TimeSpan.FromSeconds(timeout));

First create the FirefoxProfile and then add it to the capabilities:
// create the profile
var profile = new FirefoxProfile();
// create the capabilities
var capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
// start the remote driver
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);

Related

How to use existed firefox-profile in selenium webdriver 4.6

Most of the answers I found was depricated or useless.
These are what I tried to load available profile instead of instantiate a new one ( I need to use cached files to have fast tests running).
FirefoxOptions firefoxOption = new FirefoxOptions()
{
AcceptInsecureCertificates = true
};
FirefoxProfileManager firefoxProfileManager = new FirefoxProfileManager();
firefoxOption.Profile = firefoxProfileManager.GetProfile("Profile 2");
Result: a new instance of profile 2 is created and not using cached files/bookmarks/cookies of profile 2
firefoxOption.AddArgument(#"-user-data-dir=C:\tempFolder");
firefoxOption.AddArgument(#"-user-data-dir=C:\Users\Ehsan\AppData\Local\Mozilla\Firefox\Profiles\vyt729xj.appointment");
firefoxOption.AddArguments("-profile", #"C:\Users\Ehsan\AppData\Local\Mozilla\Firefox\Profiles\vyt729xj.appointment");
firefoxOption.SetPreference("-profile", #"C:\Users\Ehsan\AppData\Local\Mozilla\Firefox\Profiles\vyt729xj.appointment");
Result: useless effort and still creating a new instance of Default profile and not using previous caching(The page load time is obviously clear that all the files are being get from the net not from cache)
firefoxOption.Profile = new FirefoxProfile(#"C:/Users/Ehsan/AppData/Local/Mozilla/Firefox/Profiles/vyt729xj.appointment");
IWebDriver firefoxDriver = new FirefoxDriver(firefoxOption);
Result: exception :
Failed to set preferences: Unable to read profile preferences file
(SessionNotCreated)
How to force Selenium to use the Firefox profile to get access to previous cookies/settings/caches/bookmarks ? And not making a new instance of Firefox profile? Or at least create a new instance but use old caches/setting/bookmark/cookies?
Extra Info: C# is better but solution in other languages will be a hint.
I did it so easily using chrome and edge like this:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(#"--user-data-dir=F:\Program Files\Chrome\Profile1");
IWebDriver chromeDriver = new ChromeDriver(chromeOptions);
Update 1: Current trick is to make a new instance but use a fix address for cache:
FirefoxOptions firefoxOption = new FirefoxOptions();
firefoxOption.SetPreference("browser.cache.disk.parent_directory", #"F:\Program Files\Firefox Developer Edition\Cache");
IWebDriver firefoxDriver = new FirefoxDriver(firefoxOption);

chrome driver wont start session with given profile C#

So i've been trying to start my selenium session with my own profile and here is my code:
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=C:\\Users\\Mymsi\\AppData\\Local\\Google\\Chrome\\User Data");
options.AddArguments("--start-maximized");
IWebDriver driver = new ChromeDriver(#"C:\Users\aliza\Downloads");
driver = new ChromeDriver(options);
but for some reason selenium keeps starting a new session. Can someone tell me what am doing wrong?
Initiate driver as in below code
IWebDriver driver = new ChromeDriver(#"C:\Users\aliza\Downloads", options);

C# Selenium Start Chrome with Different User Profile

For the past 2 days, I've been trying to find a way to start Chrome with a different profile but to no avail.
No matter what I do, the profile that Selenium loads for chrome is always some temporary profile like "C:\Users\DARKBO~1\AppData\Local\Temp\scoped_dir14308_25046\Default"
I have tried the following code:
ChromeOptions options = new ChromeOptions();
options.AddArgument(#"user-data-dir=C:\SeleniumProfiles\Default");
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("chrome://version");
First I tried using the directories for the profiles directly from the Chrome folder, didn't work. Then I created a new folder and moved the profiles there, I've tried doing this both in C:\ and in D:\ . No difference whatsoever.
I've tried running the user-data-dir argument both like it currently is in the code and with -- in front of it. I've tried using double backslashes without the # symbol, still nothing. No matter what I do the profile directory is always the Selenium temp directory.
P.S. The current C:\SeleniumProfiles directory I created through the command prompt using the chrome user-data-dir=C:\SeleniumProfiles command
P.S. 2: My mistake was very simple, I forgot to put the options in the constructor of the new driver. And as Tarun made it clear, user-data-dir only gives Chrome the directory that contains the profiles, then we need to use profile-directory argument to give the subdirectory that contains the needed profile.
You din't use the options objects at all.
IWebDriver driver = new ChromeDriver();
Should be
IWebDriver driver = new ChromeDriver(options);
Edit-1 - Chrome profiles and users
Chrome has User data directory for storing profiles. Inside this directory multiple profiles can be maintained. There are two arguments that can be used
user-data-directory
profile-directory
If only user-data-directory is specified then a Default directory inside the same would be used. If profile-directory is specified then that directory inside the user-data-directory is used
If you are starting with the profile of the browser on the computer you are looking for, you can
Open normal google chrome and go to ('chrome://version')
enter link description here
Copy the Profile Path but take all of the "Data" folder and copy it to where the program is running
C# Coding:
https://rextester.com/INK23784
By creating a folder named "profile" where the program is running, you can add all the profile information, plugins, and so on. etc. We have copied the data folder in everything and when opening the browser "ChromeOptions" to selenium your profile files, etc. that's everything
You can try this code: (It worked for me)
string path_profile = #"D:\PROJECT_XMARKETING_4.0\Profiles\1";
// string path_profile = #"D:\PROJECT_XMARKETING_4.0\Profiles2\2";
IWebDriver _webDriver;
ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
cService.HideCommandPromptWindow = true;
_webDriver = new ChromeDriver(cService);
_webDriver.Manage().Cookies.DeleteAllCookies();
ChromeOptions options = new ChromeOptions();
options.AddArgument($"user-data-dir={path_profile}");
_webDriver = new ChromeDriver(cService, options);
//_webDriver.Navigate().GoToUrl("https://phamtani.com/");
//_webDriver.Navigate().GoToUrl("https://alink.vn/");
//_webDriver.Navigate().GoToUrl("http://api.hostip.info/get_json.php");
Set user-data-dir to C:\Users[your-username]\AppData\Local\Google\Chrome\User Data
Full Code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
private IWebDriver _driver { set; get; }
public YourConstructor()
{
_driver = CreateBrowserDriver();
}
private IWebDriver CreateBrowserDriver()
{
try
{
var options = new ChromeOptions();
options.AddArgument("test-type");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("no-sandbox");
options.AddArgument("disable-infobars");
//options.AddArgument("--headless"); //hide browser
options.AddArgument("--start-maximized");
//options.AddArgument("--window-size=1100,300");
//options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
// Profile [Change:User name]
options.AddArgument(#"user-data-dir=C:\Users\Haddad\AppData\Local\Google\Chrome\User Data");
var service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
return new ChromeDriver(service, options);
}
catch
{
throw new Exception("Error: Chrome is not installed.");
}
}

ChromeOptions and DesiredCapabilities relations in Selenium and C#

I've been searching for the correct documentation on how to use ChromeOptions and DesiredCapabilities in the atmosphere of Selenium and C#, but since it's all open sourced, I only find suggestions (and they are not helping sometimes). My question today is how to setup the correct relation between ChromeOptions and DesiredCapabilities. Seems like I'm doing everything correctly, but still getting System.InvalidOperationException: unknown error:cannot parse capability: chromeOptions from unknown error: unrecognized chrome option:Arguments My code is following:
private static ChromeOptions Ops()
{
var options = new ChromeOptions();
options.AddArgument("--no-startup-window");
options.BinaryLocation = #"C:\path\path\path\chromedriver.exe";
return options;
}
private static DesiredCapabilities Caps()
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability(CapabilityType.BrowserName, "chrome");
caps.SetCapability(ChromeOptions.Capability,Ops().ToCapabilities());
return caps;
}
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), Caps());
Can't find a place where incorrect Arguments are passing. Has anybody faced the same issues? This is ChromeDriver version 2.28 and selenium WebDriver v 3.3.0 Google Chrome browser version is 52.
You don't need to set the browser name; ChromeOptions does that for you.
According to this comment
The .NET bindings are moving toward a pattern where DesiredCapabilites should not be
used directly, even with RemoteWebDriver. To facilitate that, the ChromeOptions class
has a ToCapabilities() method
And there's this comment
Much like --disable-javascript, the chromedriver will not work if you use --no-startup-window.
It needs to launch a window to establish the connection with the AutomationProxy.
So that gets us to this:
var options = new ChromeOptions();
options.BinaryLocation = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
However, are you actually running a grid? If you're testing on a single machine it's even simpler:
IWebDriver driver = new ChromeDriver();

Selenium Webdriver: Specify filepath for Firefox exe

Can someone advise me on how to set the path for the firefox exe file in Selenium (C#).
I'm using the following code presently, however it is not working as hoped:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
IWebDriver driver = new FirefoxDriver(profile);
Any suggestions would be appreciated.
You should use FirefoxBinary instead of FirefoxProfile as below
FirefoxBinary binary = new FirefoxBinary(new File("path/to/binary"));
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
IWebDriver driver = new FirefoxDriver(options);
Another option is to configure the system property.
System.Environment.SetEnvironmentVariable("webdriver.firefox.bin", 'path/to/binary');

Categories