I'm trying to get started using Selenium by running some of the samples. I've seen others having trouble at run time getting the InternetExplorerDrive working, see How to instantiate InternetExplorerDriver with Selenium WebDriver using C#, but my problem is different. I get a compile time error that InternetExlorerDriver could not be found. I've installed all four "Official NuGet Packages: RC WebDriver WebDriverBackedSelenium Support" in my project.
I have the IEDriverServer.exe added to the project too but I'm not getting that far yet.
What reference am I missing?
using System;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new InternetExlorerDriver(); //missing reference error here
Compile time error:
Error 1 The type or namespace name 'InternetExlorerDriver' could not be found (are you missing a using directive or an assembly reference?) c:\users\chadd\documents\visual studio 2013\Projects\SeleniumWebDr\SeleniumTest\Program.cs 22 37 SeleniumTest
I notice that in your code, you don't have a using statement for the IE driver's namespace. Add the following line to your source code:
using OpenQA.Selenium.IE;
See if that doesn't resolve the issue.
Additionally, in your code, you're attempting to instantiate a class named InternetExlorerDriver. You're missing a p there. InternetExplorerDriver is what you want.
Incidentally, Visual Studio should provide you with tooltip support to correct this issue.
You have to download the IE Explorer driver from selenium hq site in order to invoke the Internet explorer browser as selenium only supports firefox as its default browser.
Please find below the link: http://www.seleniumhq.org/download/
download the 32 bit or 64 bit internet explorer driver server and just add the below setting in your script:
System.setProperty("webdriver.ie.driver",
System.getProperty("user.dir") + "\\IEDriverServer.exe");
Webdriver driver = new InternetExplorerDriver();
assuming you have kept the exe file in the same project folder. hope this helps !!
Related
I created a very simple C# project to test Selenium and Chrome Driver, but when I run it, it gets stuck at the very first line, ie. creating ChromeDriver object.
I have set up as following:
.NET Core 3.1 console app (also tried .NET 4.7)
Installed Selenium.WebDriver 3.141.0 and Selenium.WebDriver.ChromeDriver 87.0.4280.8800.
My Chrome version is "Version 87.0.4280.88 (Official Build) (64-bit)". I have double-checked compatibility with Chrome and ChromeDriver.
Code:
using OpenQA.Selenium.Chrome;
namespace MyFirstSelenium
{
class Program
{
static void Main(string[] args)
{
ChromeDriver chrome = new ChromeDriver();
chrome.Navigate().GoToUrl("https://www.google.co.jp/");
}
}
}
visual studio screen
chromedriver.exe is copied to the debug folder as expected, so I assume the problem is neither compatibility nor path.
chromedriver
When I run the code, the result is an empty window with 'data' in the address bar. No error is thrown. I have searched for several hours and found some articles, but most of them are old and none of these answers led to success.
Any help is appreciated. Thank you.
Added chromedriver.exe verbose console output.
#Piotr M.
chromedriver console
logfile
https://drive.google.com/file/d/1ECOS8E55aaTFV63e8P-7n6uRVcf49PRN/view?usp=sharing
New code
Works when I first execute "chromedriver.exe --verbose --log-path=chromedriver.log", but without it, it throws WebDriverException.
OpenQA.Selenium.WebDriverException: 'A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL localhost:9515/session. The status of the exception was UnknownError, and the message was: No connection could be made because the target machine actively refused it. No connection could be made because the target machine actively refused it.'
var chromeOptions = new ChromeOptions();
chromeOptions.BrowserVersion = "87";
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:9515"), chromeOptions);
Possibly a classic case of cyclic redundancy.
chrome is a reserved word, you may have to change the variable name to something else e.g. chromex. So your effective line of code will be:
ChromeDriver chromex = new ChromeDriver();
Additional Consideration
Consider the following aspects aswell:
Instead of using the ChromeDriver class use the IWebDriver interface.
IWebDriver driver = new ChromeDriver();
Instead of the partial url google.co.jp/ you need to pass the complete url through GoToUrl() as follows:
driver.Navigate().GoToUrl(#"https://www.google.co.jp/");
VS never ceases to amaze me. Or, rather, it never ceases to amaze me how I either forget how to do things between each time I create a new solution, or how things seem to change in between solutions.
I'm trying to use ChromeDriver with Selenium (in C#). I've installed the Selenium.Webdriver and Selenium.Chrome NuGet packages in the project, and everything seems fine. No errors on the using directives, and no errors when trying to create a new ChromeDriver.
Here's the Selenium driver code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;
...
public class GCDriver {
public static IWebDriver Instance { get; set; }
public static void Initialize () {
Instance = new ChromeDriver();
}
public static void Wait(TimeSpan timeSpan) {
Thread.Sleep((int)(timeSpan.TotalSeconds * 1000));
}
public static void Close() {
Instance.Quit();
}
}
When trying to start (initialize) the ChromeDriver as I allways do, I get this error:
Test Name: One
Test FullName: MyTests.TestUnitTest1.One
Test Source: C:\Users\149999frho\Source\Workspaces\QA\Automation\MyTests\TestUnitTest1.cs : line 17
Test Outcome: Failed
Test Duration: 0:00:00,0118783
Result StackTrace:
at OpenQA.Selenium.DriverService.FindDriverServiceExecutable(String executableName, Uri downloadUrl)
at OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService()
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor()
at Automation.GCDriver.Initialize() in C:\Users\149999frho\Source\Workspaces\QA\Automation\Automation\Selenium\GCDriver.cs:line 16
at MyTests.TestUnitTest1.Init() in C:\Users\149999frho\Source\Workspaces\QA\Automation\MyTests\TestUnitTest1.cs:line 12
Result Message: Initialization method MyTests.TestUnitTest1.Init threw exception. OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html..
The thing is, the chromedriver.exe IS located in the Solution\Project\Automation\bin\Debug folder. As it should be, since the NuGet package(s) is installed.
What am I missing here? Do I have to add any other references after installing the NuGet package? Adding the chromedriver.exe file as a reference is not possible, btw - VS complains about it not being an actual COM reference or something like that.
The references seems to be in order, though - and the references folder looks EXACTLY the same as in the other solution where things are working:
UPDATE:
I found a workaround: Adding the path to the chromedriver.exe file when creating a new instance of ChromeDriver. Instance = new ChromeDriver(). However, this is an idiotic and useless solution, as I have to use the absolute path - VS doesn't accept a relative one. When sharing a project it will be downright impossible to manage.
So the problem is that the project doesn't know where the file installed by it's own NuGet package manager resides. Argh! How is this possible with a piece of software in this price range?!
I have a super simple test script (below) to get started with WebDriver. When I run the test (C# - Visual Studio 2015), it opens up a Firefox browser and then does nothing.
There are several posts out there that talk about the following issue, which I'm also getting:
OpenQA.Selenium.WebDriverException: Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses: 127.0.0.1:7055.
But those posts regarding this problem are quite old and also have one major difference- their FF browser didn't open; mine does.
The error:
The code:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace seleniumDemo
{
[TestClass]
public class UnitTest1
{
static IWebDriver driverFF;
[AssemblyInitialize]
public static void SetUp(TestContext context)
{
driverFF = new FirefoxDriver();
}
[TestMethod]
public void TestFirefoxDriver()
{
driverFF.Navigate().GoToUrl("http://www.google.com");
driverFF.FindElement(By.Id("lst-ib")).SendKeys("Selenium");
driverFF.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
}
}
}
This question is different from what's been suggested as a duplicate because the FireFox browser actually opens in this case. In the other questions, it wasn't responding at all.
it seems to be related to Selenium and Firefox version incompatibility. I also faced the same error when selenium on my machine was unable to communicate to firefox. I upgraded firefox to 46.x and it started working.
You can find version compatibility information over web or refer selenium changelog as well.
Use MarrioneteDriver to use latest version of Firefox.
Below is the Java code, you can write in C# accordigly (Make sure you have geckodriver.exe under BrowserDriver folder in your project folder)
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/BrowserDrivers/geckodriver.exe");
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability("marionette", true);
WebDriver driver = new MarionetteDriver(cap);
You can download latest version of MarrioneteDriver from below :
https://github.com/mozilla/geckodriver/releases
And you should Marionette executable to Windows system path :
To add the Marionette executable to Windows system path you need
update the Path system variable and add the full directory to the
executable.
To do this, right-click on the Start menu and select System. On the
left-side panel click Advanced system settings and then Environment
Variables button from System Properties window. Now the only step left
to do is to edit Path system variable and add the full directory to
your geckodriver (you may need to add a semi-colon before doing this,
if not already present) and you’re good to go.
Then simply create your driver instance :
var driver = new FirefoxDriver(new FirefoxOptions());
I'm trying to set up Selenium for testing and none of my webdrivers seem to work. I have tried moving them around in the project folder and the only way I can get Visual Studios to locate them is with a #"path" statement.
The real problem is... Once Visual Studio locates the webdriver, the operation times out and I get the following exception:
An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll
Additional information: Cannot start the driver service on http://localhost:(random port number that changes every time)
I have tried restarting my computer and having the system administrator check the firewall and malware blocker logs, but neither seems to have helped (or they don't know the correct thing to look for).
I figure this is something super simple and I'm just missing it... Any help would be greatly appreciated.
Here is a copy of my code:
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
namespace SeleniumWork
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new InternetExplorerDriver(#"C:\blahblahpathstring");
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Cheese");
query.Submit();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.Title.StartsWith("cheese", StringComparison.OrdinalIgnoreCase));
Console.WriteLine("Page title is: " + driver.Title);
}
}
}
Here is a copy of the debug output I receive:
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll
I have had the same problem on my work machine, but not on my personal machine. The only difference I could attribute between the two was that I was using VS 2015 at home and VS 2017 at work.
What fixed it was I used the NuGet Package Manager for the project and downloaded the Selenium.Firefox.WebDriver by jbaranda, which uses the new marionette based web driver rather than gecko driver.
With this installed I was able to get a firefox browser up and running without any extra configuration or options:
IWebDriver driver = new FirefoxDriver();
driver.Url = "www.google.com";
Whereas before it would throw the 'Cannot start the driver service...' exception you mentioned. There are NuGet packages for other browsers which I suggest for the particular one you're using, but the only one I didn't have that issue with was IE. Hope that helps
I had the same issue, and had had no idea how to fix it. In the end I found out that the Firewall blocked the traffic to loopback. The firewall installed on the machine is McAffe.
All I did was stopping the service which manages traffic scan.
Hope it will help you.
I've been hitting this consistently on IEDriverServer and rarely on FireFox.
Fixed it twice when it happened on FireFox - first time I updated the gecko driver, second time I restarted my PC. Something in the environment's going on, maybe the driver does not fully quit sometimes, so new instantiations are being blocked?
I think mostly beginners face this problem when they are running Selenium with C# for the first time.
As i also faced this situation. As highlighted in one of the answer and let me show with images
You have installed Selenium.webDriver but you have not installed Selenium.Firefox.WebDriver
Step 1 :- Go to Nugget Manager
Step 2 :- Select Selenium.Firefox.WebDriver and install it
Now run the program once again and that problem will go away.
I was always able to launch chromedriver server locally but then I tried to do this remotely and since then I am unable to launch it. I reinstalled chrome as well the chrome driver but nothing seems to fix this. Even when I give the path of my driver it won't launch.
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Safari;
using OpenQA.Selenium.Interactions;
namespace TestWebDriver
{
class Program
{
static void Main(string[] args)
{
var driver = new ChromeDriver(#"C:\Users\laurens.putseys\Documents\Visual Studio 2015\Projects\TestWebDriver\packages\Selenium.WebDriver.ChromeDriver.2.21.0.0\driver\");
driver.Url = "http://google.be";
Console.ReadLine();
driver.Quit();
}
}
}
The error I get is the following:
An unhandled exception of type 'System.InvalidOperationException' occurred in WebDriver.dll
Additional information: unknown error: chrome failed to start
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 SP1 x86_64)
It started after I tried this:
System.Environment.SetEnvironmentVariable("webdriver.chrome.driver",#"/path/to/where/you/ve/put/chromedriver.exe"
Now I cannot launch the local either. Launching IE and Firefox local work without any problems. Any ideas? Thanks in advance!
try downloading the driver and give its path to contractor - LINK
driver = new ChromeDriver(DRIVER_PATH);
By the version number (2.21.371459) you can tell that the ChromeDriver executable has been launched by your remote Selenium server. But the ChromeDriver was obviously unable to launch the Chrome browser. Is it installed on the server? Can you launch is manually? Can you watch the remote server on a screen and see what happens? Are there any error message boxes displayed by Chrome?
Maybe uninstalling and re-installing Chrome could help!
I had the same problem. I had the browser set to always run under the administrator.
So I started Visual Studio as an administrator and the problem was solved.