C# selenium wont find webdriver from directory - c#

I cannot get selenium to find my webdrivers, wether it be firefox or chrome. I have tried downloading the dirvers manually and with the NuGet package manager, neither of them are working for me in C# but with python they work fine.
C# code that does not work. Gives out error:
Exception thrown: 'OpenQA.Selenium.DriverServiceNotFoundException' in WebDriver.dll
An exception of type 'OpenQA.Selenium.DriverServiceNotFoundException' occurred in WebDriver.dll but was not handled in user code
The file C:/webdrivers/geckodriver.exe does not exist. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases
Commeted code, found on some old stackoverflow question did not work either
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
class Scraper
{
/*
private static IWebDriver Driver { get; set; }
public static IWebDriver Init()
{
//System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", #"C:\webdrivers\geckodriver.exe");
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\webdrivers");
service.FirefoxBinaryPath = #"C:\Program Files\Mozilla Firefox\firefox.exe"; // May not be necessary
FirefoxOptions options = new FirefoxOptions();
TimeSpan time = TimeSpan.FromSeconds(10);
Driver = new FirefoxDriver(service, options, time);
return Driver;
}
*/
IWebDriver driver;
public void StartScraping()
{
//driver = Init();
driver = new FirefoxDriver(#"C:/webdrivers/");
}
}
Calling the StartScraping() from UWP applications MainPage.xaml.cs constructor
Python code that works just fine
import selenium
from selenium.webdriver import Firefox, Chrome
def startf():
driverf = Firefox("C:/webdrivers/")
driverf.get("https://www.google.com")
startf()
Some have said that I should add the webdrivers directory to PATH, that I've done as well but nothing changed
EDIT:
After returning to this problem a couple of days later and testing selenium with just a console application and it working properly there.
This would seem to be a problem with me using a blank Universal Windows Platform application instead of a Console App. Is there a way to "attach" or open a console window inside a UWP application? Or could that even fix this issue? if not, can I somehow open similar XML form window from a console application?

The best way to get rid of this is to use Path.DirectorySeparatorChar in the path instead of using / or \\.
You can use it like:
separator = Path.DirectorySeparatorChar;
path = $"C:{separator}webdrivers{separator}";
And now you can use this path while running firefox.

Related

Selenium ChromeDriver doesn't Navigate with --headless in console application c#

Good day to all! I have a problem using Selenium ChromeDriver in console application, i use code like this:
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--no-sandbox");
options.AddArgument(#"user-data-dir=G:\Data\ProfilesData\Profile1");
var driverService = ChromeDriverService.CreateDefaultService(Directory.GetCurrentDirectory());
driverService.HideCommandPromptWindow = true;
WebDriver driver = new ChromeDriver(driverService, options);
driver.Navigate().GoToUrl(URL);
Please help me understand so that the browser goes to the desired URL in the console application
I tried to remove the --headless parameter, the browser starts and goes to the desired page, this process does not work without visualization
I found a problem in using the profile, when commenting out the line with connecting the profile everything works, I found a comment on one of the sites that says about a Chrome bug, it cannot work with a profile in headless mode. But everything works fine on my local machine, it doesn't work on the server machine. It is necessary to look for some settings of the server machine.

How can we use Edge IE Mode in Selenium Grid(RemoteWebDriver)?

MS will stop supporting IE from June 2022, we are trying to find out a workaround to use Edge IE mode to do health checks we have for our IE supported applications. However, I can't see any supporting documents which show how to use Edge IE mode in Selenium Grid.
You can refer to the steps below to use Edge IE mode in Selenium Grid:
Download selenium-server-4.1.2.jar and IE Driver Server from here.
Download the corresponding version of Edge WebDriver from here (the same version as your Edge browser).
Put the paths of IE Driver Server and Edge WebDriver in Environment PATH.
Using the command prompt, navigate to the path of selenium server, then type on the command prompt to launch a hub:
java -jar selenium-server-4.1.2.jar hub
Using the command prompt, navigate to the same path and type on the command prompt to launch a node:
java -jar selenium-server-4.1.2.jar node
By default, the server will be listening on http://localhost:4444, and that’s the URL you should point your RemoteWebDriver tests.
Use the C# code below to run the automation test. The site will be opened in IE mode:
using System;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using System.Threading;
namespace WebDriverTest
{
class seleniumgrid
{
static void Main(string[] args)
{
var dir = #"E:\webdriver_path";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieOptions = new InternetExplorerOptions { };
ieOptions.AttachToEdgeChrome = true;
ieOptions.EdgeExecutablePath = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
WebDriver webdriver = new RemoteWebDriver(new Uri("http://localhost:4444"), ieOptions);
webdriver.Navigate().GoToUrl("https://www.google.com");
Thread.Sleep(3000);
webdriver.Close();
webdriver.Quit();
}
}
}
Note: Please change the paths and URLs to your owns.
Reference link: Getting started with Selenium Grid

Cannot start the driver service on localhost when using geckodriver with Firefox 50.0.1 in .NET

I'm learning Selenium from the scratch and trying to run a test case on Firefox 50.0.1 using geckodriver I installed in VS2015 by selecting Selenium.WebDriver.GeckoDriver.Win64
However, when running the test I got an exception
Cannot start the driver service on localhost
What am I missing?
I was following some tutorials and performed step by step walk-through.
This is my code:
[TestMethod]
public void WebDriverSample()
{
IWebDriver webDriver;
//IWebDriver webDriver = new InternetExplorerDriver();
//Thread.Sleep(1000);
//webDriver.Dispose();
//webDriver = new ChromeDriver();
//Thread.Sleep(1000);
//webDriver.Dispose();
webDriver = new FirefoxDriver();
Thread.Sleep(1000);
webDriver.Dispose();
}
When using Firefox 47.0.2, I did not need to use geckodriver at all and it worked just fine.
Now, since browsers are getting updated, at some point I need to start using new versions. So, I need to find out what to do in order to be able to adapt to new changes and use geckodriver
Any suggestons?

Selenium: "DevTools Request: 127.0.0.1:12583/json/version failed" upon instantiation

When I create a new chrome driver in Selenium while Google Chrome is already running AND I am referencing the users settings/data (via user-data-dir). A new Chrome window will open, but my application will hang. The ChromeDriver console will display the following error each second: DevTools Request: 127.0.0.1:12585/json/version. DevTools request failed
Screenshot:
Code to instantiate the driver:
ChromeDriverService driverService = ChromeDriverService.CreateDefaultService();
//driverService.HideCommandPromptWindow = true;
driverService.EnableVerboseLogging = true;
string path = Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%\\Google\\Chrome\\User Data");
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=" + path);
options.AddArguments("--start-maximized");
options.AddArguments("--disable-extensions");
IWebDriver driver = new ChromeDriver(driverService, options);
This will work perfectly fine in every instance if I do not try and load user settings/data. If I am trying to load user setting/data it will only work if there is no instance of Chrome running on the device already.
Versions:
Selenium v 2.47.0
ChromeDriver v 2.16.333243
Chrome v44.0.2403
What can I do to resolve this?
If anyone was looking for answer like me;
https://bugs.chromium.org/p/chromedriver/issues/detail?id=2443
This behavior is by design. Two instances of Chrome can't use the same user-data-dir at the same time. Otherwise they would try to update the same set of files and cause corruptions. So if an instance is already running, attempting to start another instance using the same user-data-dir will cause the second instance to ask the first instance to open a new window, and then the second instance exits.

cross browser testing using codedui?

i have issue in cross browser testing using codedui.
Using below code,
Process.Start("firefox", url);
BrowserWindow.CurrentBrowser = "firefox";
Browser = BrowserWindow.Launch(new System.Uri(url));
Keyboard.SendKeys("^{0}");
all code developed in IE . but now i have to execute code in firefox or chrome.I am going to execute the code in forefox.I am using this code here
Browser = BrowserWindow.Launch(new System.Uri(url));
in this line getting error like "An error occurred while connecting to Firefox".how to resolve this issue?I installed selenium components also. if i remove this line I am getting diffrent error like " Unable to find browser"...Please help.
Out of the box Visual Studio doesn't support cross browser CodedUI testing.
You're going to need to install Selenium components to allow for cross browser testing in Visual Studio.
Details on that can be found here:
http://blogs.msdn.com/b/visualstudioalm/archive/2012/10/30/introducing-cross-browser-testing-with-coded-ui-tests.aspx
Selenium components can be found here:
http://visualstudiogallery.msdn.microsoft.com/11cfc881-f8c9-4f96-b303-a2780156628d
Looks like CodedUI doesn't support playback on very many different browsers http://msdn.microsoft.com/en-us/library/dd380742(VS.100).aspx
There are some other tools out there http://watin.org/ is one, but I can't find anything myself that will really solve your problem.
Try
BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow WebApp;
WebApp.CopyFrom(BrowserWindow.LaunchUrl(new System.Uri(url)));
I set up something similar to the following and it works well (I hand code everything, bypassing the UIMap).
public class WebApp : BrowserWindow
{
private string _url;
public WebApp(string url)
{
//define search properties using this keyword so the web application can be treated as a browser
_url = url;
BrowserWindow.CurrentBrowser = "Chrome";
this.CopyFrom(BrowserWindow.Launch(new Uri(url));
}
}
You can overload the constructor, of course, by adding a parameter for the browser to use or whether to start up the browser or not.
By setting up the web application as a BrowserWindow, you can have one open and ready and the playback engine should find it. I find this helps when working on tests (in IE).
Just a reminder, you do need the Selenium plug-in and that plug-in will only work for playback, not recording.
Cheers!
In our environment, we set up the Launch() method by doing the following:
public void LaunchBrowser(string uri)
{
BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow myBrowser = BrowserWindow.Launch(new System.Uri(uri));
}
One thing to note is that if there is a firefox process already running in the background, the WebDriver will not launch a new instance, so be sure that all instances of firefox are shut down before the LaunchBrowser() is called. I've found that there are Java plugins that can keep it running in the background, so try to disable those that you don't need. Another good place to look, if you check your Task Manager and this is the case, is here.

Categories