Use chrome with Selenium 2.0 with C# - c#

I have a problem with the ChromeDriver.exe When I run the test, a Google Chrome Window is open but then an error from the chromedriver console shows up. It says something like:
[0405/175241: WARNING:scoped_temp_dir.cc<15>] Could not delete temp
dir in dtor.
Can anybody help me?....I'm Using C# by the way.
I wanned to upload an image but I couldn't because I'm a new user...
-JM

Selenium WebDriver C# in ChromeDriver:
Prerequisite: Install Visual Studio (mine is VS 2017), Google Chrome browser
Steps to follow:
Open VS 2017 and create solution/project
Write a test using C# code as below:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("Your Test URL");
Now install "Selenium.WebDriver.ChromeDriver" by following steps:
Right click on Project -> Manage NuGet Packages...
Click Browse at the top and search for ChromeDriver
Select "Selenium.WebDriver.ChromeDriver" and install it
Execute/Run your test

Instead of moving your chromedriver.exe, when creating a new instance of a ChromeDriver you may put the exe's file path as a parameter. This is the same as for IE.
For example: driver = new ChromeDriver("C:\ChromeDriverFolder"); will look for the cromedriver.exe in a folder on the C drive called 'ChromeDriverFolder'.

Related

Unit Testing Visual Studio Online Selenium ChromeDriver Exception

I use Selenium ChromeDriver in a Unit Test project to test some action on a web page ...
var chromeDriver = new ChromeDriver();
chromeDriver.Navigate().GoToUrl("https://www.google.com");
Chrome driver has been installed from nuget
Install-Package Selenium.WebDriver.ChromeDriver -Version 2.28.0.
When build my project in Visual Studio Online I receive some error:
System.InvalidOperationException: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.28.455520
Any solution to solve this?
I don't use the command line to install things for NuGet, I use the menu option in VS.
Open VS and click on your project in the Solution Explorer.
Click on Project > Manage NuGet Packages... from the menu,
See what shows as installed here and if there are any updates. You might try uninstalling and reinstall via the menu I described above and see if it helps. I use NuGet as described all the time and don't have this issue.
ChromeDriver cannot find the Chrome binary files just as the error message indicates. Please check and make sure that Chrome Browser is installed on the build agent or test machine which run the test.
In general chrome will be installed in the path - C:\Program Files (x86)\Google\Chrome\Application
So please check for installation path. If chrome is not in that path then uninstall old one and install chrome using link - http://filehippo.com/download_google_chrome/
The below Nuget packages should be installed
selenium.webdriver
selenium.webdriver.chromedriver
The below code will open chrome and navigate to google page
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Chrome;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://google.com");
}
}
}
You haven't set the path of chromedriver binary in System PATH variable, this how you can do it in code. Do it before you initialize the driver object.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

OpenQASeleniumWebDriver fails to start socket at 45000ms

I am new to Selenium and Web Driver and I have come across the following when using FireFox:
"OpenQA.Selenim.WebDriver Exception: Failed to start up socket within 45000ms. Attempted to connect to address 127.0.0.1.7055"
My Selenium Driver is version 2.53.1.0 and Firefox version is 48.0.1
I am only trying to open a page and type the google url just to make sure that my code works in Visual Studio 2015.
Any help would be much appreciated.
Thanks,
Tasos
I would like to write a few more details about the solution in case another stack overflow user comes across the same issue. The following steps worked for me:
1.Followed the link that Saurabh mentioned & downloaded the geckodriver exe file from https://github.com/mozilla/geckodriver/releases
2.Renamed the file from 'geckodriver' to 'wires'
3.Put the file at the Bin > Debug folder of my Visual Studio Unit Test project
4.Added the following lines of code in the cs file of my project:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
driver = new FirefoxDriver(service);

geckodriver.exe not in current directory or path variable, Selenium 2.53.1 + Firefox 48 + Selenium 3 Beta

Seen a lot of questions regarding Selenium 2.53.1 and Firefox 47.0.1, but none in regards to the Selenium 3 Beta release. I am attempting to use the new gecko/marionette Firefox webdrivers, but even though I have the driver location in; my environment path, Firefox install folder in programs, and give the drive location in the system environment, it will still not work correctly.
Error:
The geckodriver.exe does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases.
Using:
Selenium 2.53.1 server
Firefox 48
Selenium 3 Beta DLLs
Window 10
Example Code 1
using OpenQA.Selenium.Firefox;
public static class FirefoxInitialise
{
public static IWebDriver Driver {get; set;}
Driver = new FirefoxDriver();
}
Also attempted the below:
using OpenQA.Selenium.Firefox;
public static class FirefoxInitialise
{
public static IWebDriver Driver {get; set;}
FirefoxDriverServices service = FirefoxDriverService.CreateDefaultService();
service.FirefoxBinaryPath = #"C:\Program Files\Mozilla Firefox\firefox.exe";
FirefoxOptions options = new FirefoxOptions();
TimeSpan time = TimeSpan.FromSeconds(10);
Driver = new FirefoxDriver(service, options, time);
}
Any help or insight as to why the code still won't detect this driver would be greatly appreciated it.
Try to put the geckodriver.exe in your path: C:\Users\YourName\Documents\Visual Studio 2013\Projects\seleniump\seleniump\bin\Debug
you can find geckodriver.exe at this link:
https://github.com/mozilla/geckodriver/releases
Install the Selenium.Firefox.Webdriver NuGet package.
This will copy geckodriver.exe to the bin folder.
This solution may helps you fix problem: ( It did help me though)
public class TestResult {
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[TestInitialize]
public void SetupTest() {
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\geckodriver", "geckodriver.exe");
service.Port = 64444;
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Firefox Developer Edition\firefox.exe";
driver = new FirefoxDriver(service);
baseURL = "http://localhost:49539";
verificationErrors = new StringBuilder();
}
}
REFERENCE
Check out #juver-malpartida 's Answer
If u include the geckodriver.exe in your project and you copy it to your target directory when you compile, the webdriver works as it did in previous versions.
This is for who is the begginers ill write the short version in the below after this post :)
The easiest way first you need to download all drivers what browser you use and extract all drivers into e.g. C:\Selenium\ there and go to VisualStudio and from here add Selenium packages shown in the pictures Click here in console write this code PM>Install-Package Selenium.WebDriver after that copy your drivers directory and from windows search tab type variables and select (Edit the system environment and variables) shown pic2 in this windows you will have advanced tab at the below click Environment Variables... here you have System variables section find PATH or Path Variable and edit it be careful don't delete it!! next click new - paste directory of drivers and click all windws ok button that's all. restart your VS programm and ckeck it. After this you don't have to add director path into your Constructor like
IWebDriver driver2 = new InternetExplorerDriver(#"C:\Selenium");
One more thing don't forget to import files.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
For more Advanced IT guys.
Add into Environment Path Yours drivers directory.
VisualStudio install Selenium using NuGet package manager in console mode or how would u like.
PM> Install-Package Selenium.WebDriver
restart VS.
In your project, click on Tools --> Nuget Package Manager --> Manage NuGet Packages for Solution...
The in the open Window
Browse
Selenium.FireFox.WebDriver
Select Project and your project Name and click on install.
This is the easer form to put the driver on your Selenium Project.
I would try this:
First, make sure your C# project runs the same .NET framework version as the Client Driver's libraries (when you download them from Selenium HQ, you should see the framework version they're based on). I have 3.5 and 4.0 as of 9/15/2017, so I had to downgrade my C# project to .NET 4.0 to use the 4.0 Client Driver libraries.
In your code, when creating the Firefox Driver Service, make sure you explicitly specify the path to where your geckodriver.exe is located. See how I've added a path parameter to your call to FirefoxDriverService.CreateDefaultService:
using OpenQA.Selenium.Firefox;
public static class FirefoxInitialise
{
private static IWebDriver Driver{get; set;}
public static IWebDriver Init()
{
// I'm assuming your geckodriver.exe is located there:
// #"C:\MyGeckoDriverExePath\geckodriver.exe"
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\MyGeckoDriverExePath\");
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;
}
}
So you can use :
IWebDriver driver = FirefoxInitialise.Init();
If you have the executable in path environment variable, it likely means that it doesn't have permission to access it. As a workaround, try to run Visual Studio as administrator.
Or you could move it to somewhere that it have permission. Eg:
var service = FirefoxDriverService.CreateDefaultService(#"D:\tmp\Binaries");
service.FirefoxBinaryPath = FirefoxBinary;
var options = new FirefoxOptions();
options.SetPreference("browser.private.browsing.autostart", true);
_driver = new FirefoxDriver(service, options, TimeSpan.FromSeconds(30));
There I put the binaries in D:\tmp\Binaries\ and specified it in the code to check the geckodriver there.
I kept getting this error also & the only thing I could do to finally fix it (not sure if it's the best answer for everyone who has this issue) was I placed the geckodriver.exe in my main Library directory, then I opened webdriver.py:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py
found the line where it says:
executable_path="geckodriver", firefox_options=None,
and changed it to:
executable_path="/Library/geckodriver", firefox_options=None
I was having a Visual Studio 2017 issue where the build would fail because it was looking in a non existent directory for the geckodriver exec. I also had added it using nuget pack manager. What I found was in Visual Studio->Project->Properties->Build it works if you make the build independent of the architecture: Platform target is Any CPU & either leave the other check boxes (Prefer 32-bit, Allow unsafe code, Optimize code) all unchecked or just have Prefer 32-bit checked (which is the default on my system).
btw:my Application was a .NET Framework 4.5.2 Console Application
This solution worked for me for VS2017. Just copied the geckodriver.exe to my project folder like this:
C:\Users\pedne\Desktop\C#\FirstSolution\FirstSolution\bin\Debug

Selenium 2.53.1 does not work on FireFox 48

I am using selenium to test our websites. When I build the project, there is an Exception :-
OpenQA.Selenium.WebDriverException: Failed to start up
socket within 45000 milliseconds. Attempted to connect to the
following addresses: 127.0.0.1:7055 and the problem is from the code
IWebDriver driver = new FirefoxDriver();
Anybody knows how to solve this problem?
Just like the other drivers available to Selenium from other browser vendors, Mozilla has released an executable that will run alongside the browser.
You can download the latest executable geckodriver from here
Add downloaded executable geckodriver to system path
The Selenium client bindings will try to locate the geckodriver (or wires) executable from the system path. You will need to add the directory containing the executable to the system path.
On Unix systems you can do the following to append it to your system’s search path, if you’re using a bash-compatible shell:
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
On Windows you need to update the Path system variable to add the full directory path to the executable geckodriver. The principle is the same as on Unix.
After all above stuff you need to Initialize FireFoxDriver as below :-
var driver = new FirefoxDriver(new FirefoxOptions());
Note :- Follow this link for the solution of this problem with other programming language.
This answer didn't worked with me. Running selenium 2.53.6 and firefox 47 n 48.
I would recommend downloading firefox 46 which seems to be the best match for selenium 2.53.x.
https://ftp.mozilla.org/pub/firefox/releases/46.0.1/win64/en-US/
Once I downgraded to firefox 46.0.1 everything was working as expected.

How to install Selenium for Python and C#

I'm struggling to follow the directions for installing Selenium on a windows machine and I need it for both Python 7.5 and C# platforms.
I ran the command :
D:\selenium-2.37.2> D:\Python27\python setup.py install
And I get this:
Traceback (most recent call last):
File "setup.py", line 17, in <<module>>
from setuptools import setup ImportError: No module named setuptools
Setuptools? Errr... What is that? Of course I looked it up, but there is no way to get that in view of all that is advised.
Is there a sure-way of installing Selenium on Win 7 for python?.
Have you tried this Python 3: ImportError "No Module named Setuptools" ?
According to this post, it might work even better with easy_install.
Couldn't install pip and selenium in windows7
Windows 7:
1. Install PyCharm education Edition IDE with Python 2.7 (JetBrains: https://www.jetbrains.com/pycharm-educational/)
2. Setup easy install (windows - simplified)
a. download ez.setup.py (https://bootstrap.pypa.io/ez_setup.py) from 'https://pypi.python.org/pypi/setuptools'
b. move ez.setup.py to C:\Python27\
c. open cmd prompt
d. cd C:\Python27\
e. C:\Python27\python.exe ez.setup.py install
3. Setup selenium webdriver for python
a. Download selenium source code from pypi : selenium-2.45.0.tar.gz from https://pypi.python.org/pypi/selenium
b. unzip it using 7Z software
c. open cmd prompt
d. cd to selenium-2.45.0 unzipped directory
e. C:\Python27\python.exe setup.py install
4. Test selenium webdriver for python
a. Open PyCharm IDE
b. create a new python file with the following contents:
from selenium import webdriver
browser=webdriver.Firefox()
browser.get('http://seleniumhq.org/')
c. Run the python script
d. Firefox browser will open and navigate to the intended website
5. Done!

Categories