Selenium - How to hide command prompt window - c#

I am trying to hide command prompt window when starting selenium chrome, but I can't figure it out.
Here is my code:
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless");
var browser = new ChromeDriver(driverService, chromeOptions);
But it crash on: System.InvalidOperationException: 'session not created: This version of ChromeDriver only supports Chrome version 85
I tried to manually add path to driver but It crash's also.
Different code I used
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("--headless");
chromeOptions.BinaryLocation = "C:\\Users\\TriX\\Downloads\\chromedriver_win32";
var browser = new ChromeDriver(driverService, chromeOptions)
Now it crash on: OpenQA.Selenium.WebDriverException: 'unknown error: Failed to create Chrome process.'
Thanks for help I am completely lost :(
I tried different codes as shown, but without success.

Thanks to #r000bin i managed to resolve the problem. I also find out that I installed outdated version of ChromeDriver. So, I downloaded the correct one and it started to work.
I had old ChromeDrive nuget version
Downloaded the new one and uninstalled old one
Started to work :)

Related

ChromeDriver v90 does not give correct URL when connecting to remote-debugging port through the ChromeDriverService

We use ChromeDriver in C# to connect to existing instances of Chrome that have the remote debugging port 9222 set. Here is how we connect:
var svc = ChromeDriverService.CreateDefaultService(path);
ChromeOptions options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:9222";
var driver = new ChromeDriver(svc, options);
var url = driver.Url;
The problem is that the value of driver.Url is not what it used to be when using ChromeDriver version 88.
At that point and all earlier versions, driver.Url was the value of the URL for the current active tab in Chrome. So if Chrome had five tabs open and tab 4 is active, the Url was that of tab 4. And that made sense.
Once we upgraded to version 90 that is no longer the case. It appears that the value of Url is... well it's not clear. Sometimes the last active tab, sometimes some other tab, sometimes the first. I do not see a pattern.
Is this an error in ChromeDriver? In the past, whatever was the active tab was the one that driver.Url yielded. Now it's indeterminate which wreaks havoc with our code.
Update: If I have two tabs open, then the driver.Url and driver.Title are for the tab that was just prior active. So always the other tab. With 3 tabs it may be the 2nd to the last active tab. This feels like a off-by-one error within an internal array of tabs.
I had the same problem. I solved it using the method suggested here
ChromeDriverService driverService =
ChromeDriverService.CreateDefaultService();
var options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:9222";
var driver = new ChromeDriver(driverService, options);
driver.SwitchTo().Window(driver.WindowHandles[0]); // Switch to the recently opened tab
MessageBox.Show("driver.Url: " + driver.Url);

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.

Disabling popups using C# in Selenium

I apologize in advance if the following question seems to be very basic, but I am very new to Selenium and I really need help.
So what I am trying to do is, I am trying to open a window popup but Chrome browser is blocking it by its own.
I am used the following code:
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("disable-popup-blocking", "true");
IWebDriver driver = new RemoteWebDriver(new Uri("http://path/to/selenium/server"), options.ToCapabilities());
But its throwing me an exception saying:
Unexpected error. System.Net.WebException: The remote name could not be resolved: 'path'.
I have tried this, But didn't help though gave me a rough idea.
Can someone please help?
(Reference: Unblocking popup using Selenium using C#)
Try as below :-
ChromeOptions options = new ChromeOptions();
chromeOptions.AddArgument("--disable-popup-blocking");
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
Edited :- if you want to use ChromeDriver instead of RemoteWebDriver try as below :
ChromeOptions options = new ChromeOptions();
chromeOptions.AddArgument("--disable-popup-blocking");
IWebDriver driver = new ChromeDriver(#"C:\path\chromedriver", options);
Hope it works...:)

selenium doesn't enter the site

I created a console application (with target: .Net Framework 4) and added the next references:
Selenium.WebDriverBackedSelenium.dll
ThoughtWorks.Selenium.Core.dll
WebDriver.dll
WebDriver.Support.dll
static IWebDriver driver = null;
if (driver == null)
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start-maximized");
driver = new ChromeDriver(#"C:\selenium\net40", options);
// it opened a new window (about:blank)
}
driver.Navigate().GoToUrl("http://www.facebook.com");
but nothing is happen.
I use: ChromeDriver 26.0.1383.0
and my chrome browser version is: 29.0.1547.62 m
this is the command line:
Started ChromeDriver
port=1866
version=26.0.1383.0
log=C:\Users\salon\Desktop\Application Alon\ConsoleApplication1\ConsoleApplicati
on1\bin\Debug\chromedriver.log
[156:4144:0828/233852:ERROR:platform_thread_win.cc(127)] NOT IMPLEMENTED
[5804:5712:0828/233856:ERROR:textfield.h(173)] NOT IMPLEMENTED
I have windows 7 if it's needed..
any help appreciated!
For the later versions of Chrome (27+), there is a new shiny ChromeDriver:
http://code.google.com/p/chromedriver/downloads/list
Note the section within the summary on the page...
ChromeDriver server for win32 (v2.2.215849.dyu) supports Chrome v27-30
Also, you should remove the Thoughtworks & WebDriverBackedSelenium references, they are not required and you don't seem to be using them.

Categories

Resources