Search code examples
c#seleniumselenium-webdrivermicrosoft-edgebrowser-history

Keeping a persistent browser history on Selenium


I'm running Selenium 4.0.0-beta1 on MSVC 2019 using C#.

I'm using MicrosoftDriver version 17.17134.0.

Whenever I use Selenium to perform automated browsing, the browsing history is not saved after the session. I would like to keep a persistent browsing history state.

I got some answers from, Save and load browser history for selenium.

But I'm not sure how to replicate the Java solution for Firefox in C# for Microsoft Edge.

In particular, I couldn't find the equivalent profile object to import or create a profile for the browser history.

var edgedriverservice = EdgeDriverService.CreateDefaultService();
var edgeoptions = new EdgeOptions();
var driver = new EdgeDriver(edgedriverservice, edgeoptions);

I have setup my new driver object in the following manner, I'm not sure how the profile object fits in from the previous link.


Solution

  • Yes you're using Edge Chromium, then the WebDriver you're using is not right. You need to use the same version WebDriver as the Edge Chromium version in this page. You need to click full version directory and download Edge WebDriver version 86.0.622.56.

    The sample code to use the specific profile for Edge Chromium is like below:

    EdgeOptions edgeOptions = new EdgeOptions();
    edgeOptions.UseChromium = true;
    // Here you set the path of the profile ending with User Data not the profile folder
    edgeOptions.AddArgument(@"user-data-dir=C:\Users\yuzhou\AppData\Local\Microsoft\Edge\User Data");
    // Here you specify the actual profile folder
    edgeOptions.AddArgument("profile-directory=Profile 4");
    IWebDriver driver = new EdgeDriver(edgeOptions);
    driver.Navigate().GoToUrl("https://www.google.com");
    

    Note: Change the paths in the code to your owns.