Selenium Webdriver: Specify filepath for Firefox exe - c#

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');

Related

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);

Selenium Webdriver c# : Getting "The path is not of a legal form." while initializing FirefoxProfile

I am using below mentioned code to initialize a firefox driver with service, options and timeout.
var Service = FirefoxDriverService.CreateDefaultService(RunConfig.DriverPath);
Service.HideCommandPromptWindow = RunConfig.HideDriverCommandPromptWindow;
var Options = new FirefoxOptions();
if (!string.IsNullOrWhiteSpace(RunConfig.PathToBrowserBinary))
Options.BrowserExecutableLocation = RunConfig.PathToBrowserBinary;
Options.Profile = new FirefoxProfile();
Options.Profile.SetPreference(Preference.Name,PathToDownloadFolder));
Options.Profile.SetPreference(Preference.Name, Preference.Value);
return new FirefoxDriver(Service, Options, TimeSpan.FromSeconds(90));
But it gives me an error that The path is not of legal for while initializing firefox profile
Solved it. The problem was the seleniumwebdriver dll I was using is present in some other project. Adding the selenium webdriver and support dll to the current project worked.

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.");
}
}

how to use a FirefoxProfile with a IWebDriver

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);

Where to find chromedriver.log in selenium using c#. Where can i see the log file of chromedriver?

Where to find chromedriver.log in selenium using c#. Where can i see the log file of chromedriver?
ChromeOptions optn= new ChromeOptions();
optn.AddArgument("--verbose");
optn.AddArgument("--log-path=D:\\chromedriver.log");
var driver = new ChromeDriver(#"D:\Driver\",optn);
driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=aWh0U7WHEJGAuASTuYHIAQ");
I'm using the above code but unable to see the log file in specified location. Please help me to find it
I think what you're looking for is something like this:
var optn = new ChromeOptions();
var service = ChromeDriverService.CreateDefaultService(#"D:\Driver\");
service.LogPath = "chromedriver.log";
service.EnableVerboseLogging = true;
var driver = new ChromeDriver(service, optn);
driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=aWh0U7WHEJGAuASTuYHIAQ");
The ChromeOptions is for the browser process itself. Logging goes to the ChromeDriver by setting the ChromeDriverService variables.
I've found that it works if you remove the "--" from your arguments. The library code must be adding them. So your code should look like this..
ChromeOptions optn= new ChromeOptions();
optn.AddArgument("verbose");
optn.AddArgument("log-path=D:\\chromedriver.log");
var driver = new ChromeDriver(#"D:\Driver\",optn);
driver.Navigate().GoToUrl("https://www.google.co.in/?gfe_rd=cr&ei=aWh0U7WHEJGAuASTuYHIAQ");
Most easy solution will be -
System.setProperty("webdriver.chrome.logfile", "D:\\chromedriver.log");

Categories