THIS IS C# CODE SOLUTION
CREDITS: Can't save the session of Whatsapp web with Selenium c#
- First choose a FOLDER where to save the webdriver session.
- Then if folder not exist create a new Chrome Driver Session and save it using this function:
public static IWebDriver OpenNewChrome(string FolderPathToStoreSession)
{
ChromeOptions options = null;
ChromeDriver driver = null;
try
{
//chrome process id
int ProcessId = -1;
//time to wait until open chrome
//var TimeToWait = TimeSpan.FromMinutes(TimeToWaitInMinutes);
ChromeDriverService cService =
ChromeDriverService.CreateDefaultService();
//hide dos screen
cService.HideCommandPromptWindow = true;
options = new ChromeOptions();
//options.AddArguments("chrome.switches", "--disable-extensions");
//session file directory
options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
driver = new ChromeDriver(cService, options);
//set process id of chrome
ProcessId = cService.ProcessId;
return driver;
}
catch (Exception ex)
{
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
}
driver = null;
throw ex;
}
}
- If the folder exist open again the previous Chrome session using this function:
public static IWebDriver OpenOldChrome(string FolderPathToStoreSession)
{
ChromeOptions options = null;
ChromeDriver driver = null;
try
{
//chrome process id
int ProcessId = -1;
//time to wait until open chrome
//var TimeToWait = TimeSpan.FromMinutes(TimeToWaitInMinutes);
ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
//hide dos screen
cService.HideCommandPromptWindow = true;
options = new ChromeOptions();
//session file directory
options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
//options.AddArgument("--no-sandbox");
//options.AddArgument("--headless=new");
//Add EditThisCookie Extension
//options.AddArguments("chrome.switches", "--disable-extensions");
//options.AddExtensions("\\ChromeExtensions\\editthiscookie.crx");
driver = new ChromeDriver(cService, options);
//set process id of chrome
ProcessId = cService.ProcessId;
return driver;
}
catch (Exception ex)
{
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
}
driver = null;
throw ex;
}
}
PS. If you don't want Chrome Extensions to be installed or saved uncomment this option:
options.AddArguments("chrome.switches", "--disable-extensions");
user-data-dir
for next launch which will load all cookies. To get chrome cookie file path sqa.stackexchange.com/questions/45192/… – Janerich