How to start IE in private mode in c# with seleniu

2019-02-26 10:22发布

问题:

I'm starting using selenium Webdriver with c# and Visualstudio.

I would like to start Internetexplorer in private mode. So I have not to care about old browsing data during my test.

I searched for quite a long time now to find how to do this. Sadly I could not find a solution.

Could anybody provide me with a working code snippet showing how to start IE in private mode from c# code?

I used the following command to start the browser normally:

Context.Instance.Driver = new InternetExplorerDriver();

I tried to add this to make it work:

InternetExplorerOptions options = new InternetExplorerOptions();
options.BrowserCommandLineArguments = "--private";
Context.Instance.Driver = new InternetExplorerDriver(options);

This was niether a success.

Finally, I tried this also:

InternetExplorerDriverService service = InternetExplorerDriverService.CreateDefaultService();

service.SuppressInitialDiagnosticInformation = true;
InternetExplorerOptions options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
options.BrowserCommandLineArguments = "-private";

Context.Instance.Driver = new InternetExplorerDriver(service, options);

I would like to have IE running in private mode for my tests.

回答1:

Here, InternetExplorerOptions Members page; it is stated that BrowserCommandLineArguments property only has an effect when the ForceCreateProcessApi is true. ForceCreateProcessApi default value is false.

Would you please try to set it manually?

options.ForceCreateProcessApi = true;
options.BrowserCommandLineArguments = "-private";