1

I just started playing around with Selenium and Chromedriver, and cannot figure out how to launch it without Selenium's Chrome automation extension. This is required because due to the company policy here Chrome cannot handle unpacked extensions and my test app gives me an exception due to that.

I'm trying with

ChromeOptions options = new ChromeOptions();
options.AddArguments("disable-extensions");
IWebDriver driver = new ChromeDriver(options);

however it fails as I have read the whole code cannot be under

public partial class Form1 : Form

and I would have to put part of it inside a method. I can do that, but if I put

IWebDriver driver = new ChromeDriver(options);

inside anywhere that isn't directly inside my Form1 class it then the references I have under various click events fail to see the 'driver' object I defined there.

I searched around a lot but can't figure out how to structure the code so that it can properly read the option arguments AND also create the driver object in a way that other references can find. I'd really appreciate full examples as the other related topics didn't answer it for me.

Thank you

UPDATE:

Problem is I cannot get as far to compile the code, this is all due to the requirement to start ChromeDriver with a parameter. Without the parameter it all works, but I need it unfortunately, but I'm lost at how shjould I structure the code in a way that the object is not created directly inside the class yet click events can refer to it.

--

If I try to create the object under the Form1 class, I cannot pass the parameters to it, without those it works perfect. I have read it that if I want to add parameters to ChromeDriver it cannot be simply declared under the class.

If I put in under Form1_Load event for example, it can understand the parameters and opens the browser window with those parameters, but then my click events for various buttons cannot see the ChromeDriver driver I created under Form1_load, I get "xxx does not exist in the current context".

Also tried putting it all inside a

public ChromeDriver mymethod()

method, which has

return driver;

as it's return value. In this case, I can call that method in my button click events, but since the new ChromeBrowser obkect is defined in that method, each time i click the button to do something it creates a new browser windows as the code to create the object runs again. Tried to put an IF inside the method but it fails as I cannot

return driver;

on the ELSE chain without creating a new ChromeBrowser object in that chain as well, which results in yet another windows opened.

If there is any interest I can provide the code to look at.

kenyér
  • 11
  • 2
  • Try `"--disable-extensions"` instead of `"disable-extensions"` – Chawin Oct 03 '17 at 08:34
  • Thanks for the reply Chawin, problem is i cannot get as far as compiling the code to see if the launch parameter would even work. Updated the original post with results – kenyér Oct 03 '17 at 09:15
  • Does [this](https://stackoverflow.com/a/23229927/5309534) answer help at all? – Chawin Oct 03 '17 at 09:39
  • Sort of, it gives me the parameters to use, but I got stuck on where should I put this in the code, ie where should I initialize the browser object. Cannot put it under the class, cannot put it in a load event, and cannot put it in a method either, for different reasons. Under the class it cannot read the parameters, under an event "xxx does not exist in the current context" when later I reference to it in a separate button_click event, and when called by a method it will open a new browser window on every call instead of interacting with the current one. – kenyér Oct 03 '17 at 09:58
  • Have a `driver` at the top of the `Form1` class and create a `GetDriver` method which contains a check if a driver exists. If it doesn't exist then create a new driver and assign to the top level `driver`, else return the existing `driver`. – Chawin Oct 03 '17 at 10:06

0 Answers0