I'm using selenium 3.14 with geckodriver 0.24, I'm using following code to run the existing profiles I have already created for my different accounts.
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.Proxy = pro; //my proxy object
firefoxOptions.AddArgument("-profile " + path); //path to the profile
FirefoxDriverService ffDriverService = FirefoxDriverService.CreateDefaultService();
ffDriverService.BrowserCommunicationPort = 2828;
PropertiesCollection.Driver = new FirefoxDriver(ffDriverService, firefoxOptions);
I have multiple profiles each with a different proxy. Right now, the browser is started and everything works very well for the first profile, but once I dispose the browser and start a new one with new profile and proxy, the driver opens the same last browser. I've tried many solutions and have changed selenium to old versions but no luck.
One thing I noticed in the console is that when driver opens the browser, it runs a command on console like this:
1561625708285 mozrunner::runner INFO Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile C:\\Users\\Usr\\Desktop\\fprofiles\\pf1" "-foreground" "-no-remote"
if I run this command from cmd the profile issue remains there:
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile C:\\Users\\Usr\\Desktop\\fprofiles\\pf1" "-foreground" "-no-remote"
If I remove the " from command and make it complete text it will look like this
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" -marionette -profile C:\\Users\\Usr\\Desktop\\fprofiles\\pf1 -foreground -no-remote
I cloned the selenium project of OpenQA and tried to debug there but that also uses geckodriver.exe and I guess geckodriver.exe is responsible for getting arguments and passing to firefox.
Last but the least option will be to compile geckodriver(which has been developed in RUST) once again as per my consent but the programming language is RUST and that's going to be a long long job for achieving what I need.
Has anyone faced the same problem? How can I get it fixed?
Try loading browser profile based on it's name. An example with profile called 'selenium_profile':
public static WebDriver driver;
public static String driverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\FF_driver_0_23\\geckodriver.exe";
public static WebDriver startFF() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
return driver;
}
It must not be static so you can parse name of desired profile in argument.
Related
I started using selenium with CS and have one issue. When code is compiled, program cannot find webdriver path, because it's being moved into the .exe file. I fixed this problem, by copying driver into the bin folder, so program can access it again. However, I want it to be able to access that driver inside .exe file.
I was doing this in python using os path:
def resource_path(relative_path: str) -> str:
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
If anyone knows how to do this in cs, please let me know.
Code that I'm using in c#:
var browser = new EdgeDriver();
browser.Navigate().GoToUrl(link);
webdrivermanager should be more helpful here. you can add its Nuget and use to manage drivers for browsers without requiring the driver exe files.
I use something like this and call this method everytime I need a browser.
public static InternetExplorerDriver InitBrowser(string browserName)
{
switch (browserName)
{
case "IE":
{
var IE_DRIVER_PATH = #"C:\PathTo\IEDriverServer";
InternetExplorerDriver driver = new InternetExplorerDriver(IE_DRIVER_PATH);
return driver;
}
}
return null;
}
This allows you to define the path from which to grab the driver, and so you wont have to depend on it being in your BIN folder. There are other solutions but this is what I have that works really well for me. You are set up to use this method for other browsers by adding more switch cases, and also from here you can easily add your browser options. You can call the method in your tests using:
InternetExplorerDriver driver = InitBrowser(IE);
Here it is simplified without the switch case:
var IE_DRIVER_PATH = #"C:\PathTo\IEDriverServer";
InternetExplorerDriver driver = new InternetExplorerDriver(IE_DRIVER_PATH);
I need to use a shared profile for Firefox, which doesn't get deleted on exit. Seems that this can be done using a FirefoxProfile or FirefoxOptions. But none of them seems to work: When starting the geckodriver, it uses a temp profile like this
1507646897935 mozrunner::runner INFO Running command:
"C:\Program Files\Mozilla Firefox\firefox.exe" "-marionette"
"-profile"
"C:\Users\\AppData\Local\Temp\rust_mozprofile.uzI9KAmLQ1zP"
When debugging, I noticed that the property ProfileDirectory of the profile is always null.
var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("Test");
var driver = new FirefoxDriver(profile);
The profile Test was created manually using firefox -pbefore. I also tried to use it's location like this:
var profile = new FirefoxProfile(#"C:\Users\<MyUsername>\TestProfile", deleteSourceOnClean: false);
But same problem, can't figure out why this isn't working.
Used software
geckodriver 0.19.0
Selenium.Firefox.WebDriver 2.0.0 (NuGet)
Selenium.WebDriver 3.6.0 (NuGet)
ASP.NET Core 2.0
Solved this issue by passing the path to my profile as regular CLI parameter to Chrome:
var options = new ChromeOptions();
options.AddArgument(#"--user-data-dir=C:\Users\<MyUsername>\TestProfile");
var driver = new ChromeDriver(options);
Should also work with Firefox. But I needed to switch to Chrome until another bug in the FF driver got fixed. This is no full clean solution at all, but it works as a workaround until a better solution is found.
In Firefox, I needed to preserve all the cookies, history, cache etc and nothing worked since selenium isn't built to save any of these across sessions for obvious reasons.
Since there is no solution for Firefox, here is how I hacked it
Read the firefox.exe command line in order to find out whats the
profile temp dir.
Manually close the browser so the temp profile isn't deleted
Move the temp profile with all the data preserved
Here is the code:
IWebDriver _driver;
var service = FirefoxDriverService.CreateDefaultService();
//Start webdriver
_driver = new FirefoxDriver(service, options);
//get the webdriver commandline so we can get the path of the ff profile temp dir so we can save it later
var proc = service.ProcessId;
string cmdline = GetCommandLine(proc);
string profilePath = cmdline.Substring(cmdline.IndexOf(" -profile ") + 10);
//Do stuff with your browser
//In order to move the temp profile dir, we have to 'manually' close the browser window.
//There is no other way because the temp profile gets deleted if you use _driver.close()
var ffprocess = Process.GetProcesses().FirstOrDefault(x => x.MainWindowTitle == $"{title} - Mozilla Firefox");
ffprocess.CloseMainWindow();
//Delete the old profile data so we can get the updated data.
Directory.Delete(sProfile, true);
//store the temp profile data
Directory.Move(profilePath, sProfile);
//this must be deleted, othervise the webdriver won't start next time
File.Delete(sProfile + #"\user.js");
string GetCommandLine(int process)
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT CommandLine FROM Win32_Process WHERE CommandLine Like \"% -profile %\" AND ParentProcessID = {process}"))
using (ManagementObjectCollection objects = searcher.Get())
{
return objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
}
}
We are trying to create a website verifier for our internal site, to verify that all sites are up. For this, we use seleniums chromedriver.
We have 2 projects for this:
1 main project which executes the code.
1 "shared" project, which is shared between all of our different solution. This project contains data, which is used across multiple solutions.
We have placed the chromedriver in the shared project, and initialize it like this:
public static IWebDriver InitiateChromeDriver()
{
ChromeOptions option = new ChromeOptions();
option.AddUserProfilePreference("download.default_directory", downloadPath);
option.AddUserProfilePreference("disable-popup-blocking", "true");
var path = Path.GetFullPath("Utility");
Console.WriteLine(path);
IWebDriver driver = new ChromeDriver(path, option, TimeSpan.FromMinutes(20));
return driver;
}
This method is placed in the "Utility" folder, together with the Chromedriver.exe, and can run locally when debugging through Visual Studio.
When we deploy it to our production server, it cannot find the path to the chromedriver. The referenced path changes to C:\windows\system32\inetsrv\Utility\chromedriver.exe on our production server.
What is a better approach at referencing the file, and ensuring that the path is correct?
Try the below. Create a folder called drivers and add the chromedriver to it.
ChromeOptions options = new ChromeOptions();
option.AddUserProfilePreference("disable-popup-blocking", "true");
driver = new ChromeDriver(Path.Combine(GetBasePath, #"Drivers\\"), options);
public static string GetBasePath
{
get
{
var basePath =
System.IO.Path.GetDirectoryName((System.Reflection.Assembly.GetExecutingAssembly().Location));
basePath = basePath.Substring(0, basePath.Length - 10);
return basePath;
}
}
I am working on creating a regression test suite using Selenium for IE Browser. I am using the IEDriver exe from Selenium website. As per instructions from Selenium,
"The Internet Explorer Driver Server
This is required if you want to make use of the latest and greatest features of the WebDriver InternetExplorerDriver. Please make sure that this is available on your $PATH (or %PATH% on Windows) in order for the IE Driver to work as expected."
Approach 1
I tried to setup PATH variable via batch file as follows
setlocal
set varC=%CD%\ChromeDriver
set varI=%CD%\IEDriver
set PATH=%PATH%;%varC%;%varI%
However i still face issues with IEDriver not working properly.
Approach 2
When i set PATH variable via "Advanced System Settings", everything seems to be working fine. Can someone confirm if this setting can't be done via batch file or if i am performing some wrong operation?
Here is how I am initializing driver
[OneTimeSetUp]
public void SetupTestFixture()
{
switch (ConfigPara.TestBrowser.ToLower())
{
case "ie":
Utility.KillProcess("iexplore");
DesiredCapabilities caps = DesiredCapabilities.InternetExplorer();
caps.SetCapability("ignoreZoomSetting", true);
caps.SetCapability("nativeEvents", false);
caps.SetCapability("allow-blocked-content", true);
caps.SetCapability("disable-popup-blocking", true);
caps.SetCapability("allowBlockedContent", true);
aOptIE = new OpenQA.Selenium.IE.InternetExplorerOptions();
aOptIE.InitialBrowserUrl = ConfigurationManager.AppSettings.Get("baseURL");
aOptIE.EnablePersistentHover = false;
aOptIE.RequireWindowFocus = true;
aOptIE.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
System.Environment.SetEnvironmentVariable("webdriver.ie.driver", ConfigPara.IEDriverDirectory.FullName +"\\IEDriverServer.exe");
Utility.Instance.driver = new InternetExplorerDriver(ConfigPara.IEDriverDirectory.FullName, aOptIE);
break;
}
Utility.Instance.driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(7));
baseURL = ConfigPara.BaseURL;
Utility.Instance.wait = new OpenQA.Selenium.Support.UI.WebDriverWait(Utility.Instance.driver, TimeSpan.FromSeconds(30));
//utility = new Utility(driver);
}
[OneTimeTearDown]
public void SetupTestTeardown()
{
try
{
Utility.Instance.driver.Quit();
Utility.Instance.driver.Dispose();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
Hi actually you have to set the path of the IE driver that you downloaded form here http://docs.seleniumhq.org/download/
please do it like below
System.setProperty("webdriver.ie.driver","pathofIEdriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
User below code for C# webdriver:
System.Environment.SetEnvironmentVariable("webdriver.ie.driver", "Path\IEDriverServer.exe");
You can use webdriver manager class to handle it.
WebDriverManager.iedriver().setup();
Added in your maven pom file :
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
</dependency>
You can also use Webdrivermanage dependency for Gradle. This will automatically maintain IE driver exe file.
I try to load ChromeDriver with adblock, and somehow it reloads downloading the extension everytime it runs and shows this message:
If you see this message every time you start AdBlock, please make sure you are not using a file cleaner that also cleans 'localStorage' files.
var options = new ChromeOptions();
options.AddArgument("--no-experiments");
options.AddArgument("--disable-translate");
options.AddArgument("--disable-plugins");
options.AddArgument("--no-default-browser-check");
options.AddArgument("--clear-token-service");
options.AddArgument("--disable-default-apps");
options.AddArgument("--no-displaying-insecure-content");
options.AddArgument("--disable-bundled-ppapi-flash");
options.AddExtension(#"D:\AdBlock-v2.6.5\adblock.crx");
using (IWebDriver driver = new ChromeDriver(options))
{
driver.Navigate().GoToUrl(url);
}
Try to use the same chrome profile on every run. This must resolve the issue.
Code to do this located here: Load Chrome Profile using Selenium WebDriver