I have created a new user profile(In Run, type -> firefox.exe -p and create a new profile). For demo purpose I have created a new folder on my desktop. My new user profile points to this new location (C:\Users\username\Desktop\TemporaryProfile). In my selenium webdriver i want to change the default location of the firefox profile.
FirefoxBinary binary = new FirefoxBinary(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe");
FirefoxProfile profile = new FirefoxProfile(#"C:\Users\username\Desktop\TemporaryProfile");
FirefoxDriver driver = new FirefoxDriver(binary, profile);
In the preceding code, it is not taking the new firefox profile location rather it is taking the existing one. I mean the default location.
And FireFoxProfile Class has a property named profileDirectory. But unfortunately it is a get property
public string ProfileDirectory { get; }
So I couldn't be able to set the directory location.
How to change the firefox profile location in selenium webdriver? Whenever my script executes it should not take the default profile location rather it should point to the new user profile location. Can anyone tell me how to do this?
This works for me to change profile:
var option = new FirefoxOptions();
var profile = new FirefoxProfile("Path_to_profile");
option.Profile = profile;
IWebDriver driver = new FirefoxDriver(option);
Related
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);
I manually created a specific firefox profile containing an add-on/extension (Hotspot Shield VPN add on). Now, in code I am loading this profile via the firefox driver, however the problem is that the extension is not being loaded when launched from code. On the other hand it works just fine when launching the profile manually.
var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("brysontiller");
profile.SetPreference("extensions.allowPrivateBrowsingByDefault", true);
profile.SetPreference("extensions.hotspot-shield#anchorfree.com.onByDefault", true);
var options = new FirefoxOptions { Profile = profile, LogLevel = FirefoxDriverLogLevel.Trace };
var driver = new FirefoxDriver(#"C:\Users\danza\source\repos\InstaManager\", options);
driver.Navigate().GoToUrl("https://www.google.com");
I tried adding the extension in code by using
profile.AddExtension(#"C:\Users\danza\AppData\Roaming\Mozilla\Firefox\Profiles\652o40ny.profile_7\extensions\hotspot-shield#anchorfree.com.xpi");
The above problem still persists with this line of code. (Although this is not really needed as the manually created profile already contains the needed extension so no need to add it again).
I also tried setting various profile preferences as shown below:
profile.SetPreference("extensions.allowPrivateBrowsingByDefault", true);
profile.SetPreference("extensions.hotspot-shield#anchorfree.com.onByDefault", true);
Problem still persists.
Add-on loads when I launch the profile manually as shown below:
But when I launch it via code, it does not:
If you manually created the profile why don't you try to open the browser using it directly:
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.Profile = new FirefoxProfile("Path to the profile");
driver = new FirefoxDriver(..., firefoxOptions);
Could you try and let me know?
I am trying to open more then one instance of tor browser with different identities with c#.
One of the brwoswers opens fine:
But when the code opens the second I get empty browser:
Here is my c# code:
IWebDriver browser;
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9150);
browser = new FirefoxDriver(profile);
// browser.Quit();
//Firefox's proxy driver executable is in a folder already
// on the host system's PATH environment variable.
browser.Navigate().GoToUrl("http://dnsleaktest.com");
//IWebElement header = browser.FindElement(By.Id(“site - header"));
//Assert.True(header.Displayed);
// browser.Close();
Thread.Sleep(10000);
IWebDriver browser2;
FirefoxProfile profile2 = new FirefoxProfile();
profile2.SetPreference("network.proxy.type", 1);
profile2.SetPreference("network.proxy.socks", "127.0.0.1");
profile2.SetPreference("network.proxy.socks_port", 9152);
browser2 = new FirefoxDriver(profile2);
// browser.Quit();
//Firefox's proxy driver executable is in a folder already
// on the host system's PATH environment variable.
browser.Navigate().GoToUrl("http://dnsleaktest.com");
//IWebElement header = browser.FindElement(By.Id(“site - header"));
//Assert.True(header.Displayed);
// browser.Close();
Please correct my English if needed
NuGet:
NUnit3TestAdapter
List itSelenium.WebDriverem
Change this line:
//Firefox's proxy driver executable is in a folder already
// on the host system's PATH environment variable.
browser.Navigate().GoToUrl("http://dnsleaktest.com");
//IWebElement header = browser.FindElement(By.Id(“site - header"));
To this:
//Firefox's proxy driver executable is in a folder already
// on the host system's PATH environment variable.
browser2.Navigate().GoToUrl("http://dnsleaktest.com");
//IWebElement header = browser.FindElement(By.Id(“site - header"));
You were navigating the first browser object when you needed to be navigating the second browser object.
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.");
}
}
I am writing a program to run videos listed on my site for testing purpose and here what I need is to run videos in different tabs of the same browser window.
I have hundred video urls in the List videoLinks = getVideoUrls();
and now what I need is to execute these videos 5 at a time.
ChromeDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.withoutabox.com" + videoLink);
If I go the above way then for all videos I will have to create a new ChromeDriver object. I want to use single chrome browser object.
I have tried this
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
it only adds a new tab but not open a link there.
Please let me know how should I go around it. I have googled but couldn't find my solution so thought to ask for help.
Try this:
public void SwitchToTab(object pageId)
{
webDriver.SwitchTo().Window(pageId.ToString());
}
You can use CurrentWindowHandle to find current tab.
webDriver.CurrentWindowHandle;
For your scenario I'm using that code:
public IPageAdapter OpenNewTab(string url)
{
var windowHandles = webDriver.WindowHandles;
scriptExecutor.ExecuteScript(string.Format("window.open('{0}', '_blank');", url));
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo().Window(openedWindowHandle);
return new SeleniumPage(webDriver);
}
Update
Window open create new popup. By default this option can be blocked by browser settings. Disable popup blocking in your browser manually.
To check this, open js console in your browser and try to execute command window.open('http://facebook.com', '_blank');
If new window open successfully than everythng is OK.
You can also create your chrome driver with specific setting. Here is my code:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("profile.default_content_settings.popups", 0);
return new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromSeconds(150));
Here is a simple solution for open a new tab in seleneium c#:
driver.Url = "http://www.gmail.net";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");