Remote Webdriver Chrome throws a "path to the driver executable" error - c#

Hi when i use the following code
IWebDriver _webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),
DesiredCapabilities.Chrome());
I get the follwing error
System.InvalidOperationException : The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at Testframework.Browser.RemoteGoto(String browser, String url) in Browser.cs: line 86
at Testframework.CommonAction.RemoteBrowser(String browser) in CommonAction.cs: line 70
at Test.RegistrationTest.InvalidRegistrationTest(String browser, String username, String password, String confirmPassword, String securityQuestion, String securityAnswer, String errorMessageText, String firstname, String lastname) in RegistrationTest.cs: line 50
--TearDown
at Testframework.CommonAction.CaptureScreen(String fileName) in CommonAction.cs: line 121
at Test.RegistrationTest.SnapshotOnFailure() in RegistrationTest.cs: line 590

The clue really is in the error.
Chrome should be installed on the system where the tests are either running on or being pointed to.
Take a step back, look at the documentation:
https://code.google.com/p/selenium/wiki/ChromeDriver
Also, if Chrome is installed in a peculiar place, you'll need to point Selenium to it's location. Again, this is explained in the documentation.
In C#:
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.binary", this.binaryLocation);
or:
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = "pathtogooglechrome";
capabilities.SetCapability(ChromeOptions.Capability, options);

Instead of changing the code you can have other way round.
Download the chrome driver and set the PATH environment variable pointing to the directory where the chromedriver.exe is present.
Restart your IDE / Command console and run the tests. It works!!!

Related

Firefox opening wrong profile with selenium c#

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.

Selenium geckodriver executes findElement 10 times slower than chromedriver (.Net)

Sorry didn't find a similar question and maybe somebody can help.
Due to additional requirements we have to test our project not only with Chrome but with Firefox as well. When we simply changed a test context to Firefox it turned out that all calls of findElement took 10 times more time than with Chrome. All tests are completely ruined. We tried to use different test machines but the results are the same. The project is on Core .Net. For testing we use MSTest V2, Firefox 63 (64 bit) and Geckodriver 0.22 (64 bit) .
Very appreciate any help.
By referring to the previous answer, my issue was solved by below code.
string geckoDriverDirectory = "Path of geckodriver.exe"
FirefoxDriverService geckoService =
FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
geckoService.Host = "::1";
var firefoxOptions = new FirefoxOptions();
firefoxOptions.AcceptInsecureCertificates = true;
Driver = new FirefoxDriver(geckoService, firefoxOptions);
Yep. You’re definitely hitting the performance issue that is part of .NET Core. It doesn’t happen on Chrome, IE, or Edge, because the driver executables for each of those browsers (unlike geckodriver) listen on both the IPv4 and IPv6 loopback addresses. If you were to specify “::1” as the host for geckodriver with .NET, the problem would disappear.
Refer to https://github.com/SeleniumHQ/selenium/issues/6597
A complete .Net Core webdriver for Firefox 7/14/2020:
// .Net Core workaround #1: Slow Firefox webdriver
string projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).FullName;
string geckoDriverDirectory = projectFolder + "\\netcoreapp3.1\\";
FirefoxDriverService geckoService =
FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
geckoService.Host = "::1";
var ffOptions = new FirefoxOptions();
ffOptions.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\Firefox.exe";
ffOptions.AcceptInsecureCertificates = true;
// This profile will by-pass *ALL* credentials. Note that Chrome uses Internet Explorer settings, so it does not need this.
// You must pre-setup the profile, by launching Firefox and doing phone authentication
// The profile's location is: C:\Users\<windows logon>\AppData\Local\Mozilla\Firefox\Profiles
// Without this, if your AUT uses SSO, you will always get prompted for the PIN or Two factor authentication
FirefoxProfile profile = new FirefoxProfileManager().GetProfile("Selenium");
ffOptions.Profile = profile;
// DotNet Core workaround #2- Code page
// https://stackoverflow.com/questions/56802715/firefoxwebdriver-no-data-is-available-for-encoding-437
// https://stackoverflow.com/questions/50858209/system-notsupportedexception-no-data-is-available-for-encoding-1252
CodePagesEncodingProvider.Instance.GetEncoding(437);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
_driver = new FirefoxDriver(geckoService, ffOptions);
In case anyone is trying gary.zhang's answer in Javascript, it looks like this:
let driver = new Builder()
.forBrowser('firefox')
.setFirefoxService(new firefox.ServiceBuilder('path_to_driver', host='::1'))
.setFirefoxOptions(new firefox.Options().headless())
.build();
Took me a bit of staring at it to figure out how to convert the syntax.

EdgeDriver - Cannot change window size in Edge

I am using the EdgeDriver for running automation tests on my browser (Edge 38.14393.0.0). My tests are in C#, so I am using the .NET driver:
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Edge;
var options = new EdgeOptions();
options.PageLoadStrategy = EdgePageLoadStrategy.Normal;
RemoteWebDriver driver = return new EdgeDriver(Environment.CurrentDirectory, options, TimeSpan.FromSeconds(60));
driver.SetDocumentSize(new Size(800, 600)); // HERE!
The error
This code is the one I run at the beginning of the test. And it fails at the last line with:
Class Initialization method
Web.TestSuite.UIRendering.RenderingTestSuiteEdge.TestClassInitialize
threw exception. System.InvalidOperationException:
System.InvalidOperationException: A window size operation failed
because the window is not currently available.
With this stack trace:
OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs: line 1126
OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\webdriver\dotnet\src\webdriver\Remote\RemoteWebDriver.cs: line 920
OpenQA.Selenium.Remote.RemoteWindow.set_Size(Size value) in ...
FYI Be aware that I have other tests running on Chrome and IE11 using their respective drivers. When I call SetDocumentSize on those, I get no errors.
Open issues
I could find some open issues related to this problem:
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9340417/
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8778306/
Questions
So, these are my questions:
Has anybody succeeded in setting the window size in Edge?
Is this problem I am hitting a known issue? If so, is it fixed? The referenced issues (which look similar) are still open and no status provided.
Is there any workaround?
Try one of those for C#:
driver.Manage().Window.Size = new Size(1920, 1080);
driver.Manage().Window.Maximize();
Although I encounter that error for different reason (like the one here -> https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10319887/).
Now I kill all process of the driver & edge before each test so hopefully it will be resolved like that:
try
{
foreach (var process in Process.GetProcessesByName("MicrosoftWebDriver"))
{
process.Kill();
}
foreach (var process in Process.GetProcessesByName("MicrosoftEdge"))
{
process.Kill();
}
}
catch (Exception)
{
}
Also if you run them on remote machine via RDP for example it will make the same error when you close the RDP. This is the current workaround that I found for it:
Create a batch file with this code:
for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
%windir%\System32\tscon.exe %%s /dest:console
)
Create a desktop shortcut to this file. To do this, right-click the batch
file and select Send to | Desktop (create shortcut).
In the shortcut properties, click Advanced and select Run as administrator.
Now, when you need to disconnect from Remote Desktop, double-click this shortcut on the remote computer (in the Remote Desktop window).
Thanks to https://support.smartbear.com/testcomplete/docs/testing-with/running/via-rdp/keeping-computer-unlocked.html for the script.
This works for me:
driver.manage().window().setSize(new Dimension(1250, 720));

Why chromedriver redownload the adblock extension on all runs?

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

Selenium server start within .NET

I'm using Selenium to control Opera within C#. I'm using selenium-server-standalone-2.33.0 .
When i start the server from command line there is no problem . My code work well.
But I need to start the server from C# and I can start it with a bat file execution. I can start the server and create a driver.
(I'm using "java -jar selenium-server-standalone-2.33.0.jar -trustAllSSLCertificates" command to start the server in both cases.)
My problem is:
If the server started from C# code, my code can't find the element and throwing exception: (Driver's page source property contains xxx element.)
My Code which throws the exception:
element = driver.FindElement(By.Id("xxx"));
All properties of the element throw an exception.
I think it's because of the process.start privileges when i start server. I've searched a lot but i couldn't find anything.
Thanks
I can start sever with this command "java -jar selenium-server-standalone-2.33.0.jar -trustAllSSLCertificates" from command line and C# code. Server starting well i can create driver and can see the page source .
IWebDriver driver = new RemoteWebDriver(new System.Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Opera());
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(100));
But when i want to reach WebElement through this code element throws lots of exception ('element.Displayed' threw an exception of type 'System.InvalidOperationException')
IWebElement element; // FindElement(driver, "txtUserName", 100);
element = driver.FindElement(By.Id("txtUserName"));
string name = element.GetAttribute("Name");

Categories

Resources