-1

I am creating a console application using Selenium web driver c# and I have a folder in my project which contains the Chromedriver.exe. I want to give the path of that folder in my file. What should I do for this?

IWebDriver driver = new ChromeDriver(@"C:\Users\sharmaash\Documents\Software\chromedriver_win32")

I am using this in my code, what changes I need to make in the above mentioned code line?

The screenshot of my local folder which I need to access: enter image description here

I want to give a path of chromedriver.exe in: IWebDriver driver = new ChromeDriver(@"C:\Users\sharmaash\Documents\Software\chromedriver_win32") rightnow I am using my local system path. The reason behind is I want to create an exe file of my application and for that I want to give relative path.

Ashish Sharma
  • 131
  • 1
  • 5
  • 19
  • 1
    Possible duplicate of [How can I get the application's path in a .NET console application?](http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application) – S.Dav Feb 03 '16 at 08:49
  • Thanks, I have made some changes in my question. Can you please suggest on it. – Ashish Sharma Feb 03 '16 at 09:02
  • can you put your folder structure and where this driver is put so we can help u ? – ItsZeus Feb 03 '16 at 09:50

2 Answers2

1

I recommend using NuGet to manage dependencies. It will automatically copy the chromedriver.exe to output folder. NuGet install chromedriver

Another solution is to add the chromedriver.exe to project then set property "Copy to Output Directory" as Copy if Newer. Add chromedriver.exe to project

Set value to Copy if Newer

Then you can create ChromeDriver by:

IWebDriver driver = new ChromeDriver();
Buaban
  • 5,029
  • 1
  • 17
  • 33
  • Thanks, but I am using Visual Studio 2008. – Ashish Sharma Feb 03 '16 at 09:27
  • Then you can use the second solution which is adding the chromedriver.exe to project then set property "Copy to Output Directory" as Copy if Newer. – Buaban Feb 03 '16 at 09:28
  • I do it like this, too (the 2nd solution). Works perfectly fine. No need to specify the path when instantiating the ChromeDriver, as it is in the same path as your compiled binary. – Kim Homann Feb 03 '16 at 10:00
0

You can keep the path in a config file instead of hard coding it and fetch it from there. Secondly you can get path of your application code base, where your application code resides by using following statement and provide it to your ChromeDriver Constructor

System.Reflection.Assembly.GetExecutingAssembly().CodeBase

Dynamic Path Get e.g.

string driverPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; IWebDriver driver = new ChromeDriver(driverPath);

ItsZeus
  • 142
  • 12
  • I just placed this code on my file: `string driverPath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; IWebDriver driver = new ChromeDriver(driverPath);` Now where should I add the "\chromedriver_win32" path? – Ashish Sharma Feb 03 '16 at 09:26
  • can you put your folder structure and where this driver is put so we can help u ? – ItsZeus Feb 03 '16 at 09:42
  • I have updated the question with the screenshot. Please suggest – Ashish Sharma Feb 03 '16 at 09:50