C# 使用selenium(cookies)保存firefox会话

C# 使用selenium(cookies)保存firefox会话,c#,selenium,firefox,cookies,selenium-webdriver,C#,Selenium,Firefox,Cookies,Selenium Webdriver,我正在尝试保存c#中selenium驱动的firefox会话中的cookie。我知道我可以通过序列化每个网站的每个cookie,然后将这些cookie添加回该网站,但这并不是最好的解决方案,因为: 1.我无法在加入网站之前设置cookie,因此我需要先访问该网站,然后添加cookie,但该网站已在访问时设置cookie,因此我需要删除它们并设置我的cookie,这可能会导致网站出现问题。我宁愿让firefox和site处理cookies。 2.我也需要担心过期的饼干 我已经试过复制临时文件了。s

我正在尝试保存c#中selenium驱动的firefox会话中的cookie。我知道我可以通过序列化每个网站的每个cookie,然后将这些cookie添加回该网站,但这并不是最好的解决方案,因为: 1.我无法在加入网站之前设置cookie,因此我需要先访问该网站,然后添加cookie,但该网站已在访问时设置cookie,因此我需要删除它们并设置我的cookie,这可能会导致网站出现问题。我宁愿让firefox和site处理cookies。 2.我也需要担心过期的饼干 我已经试过复制临时文件了。selenium创建的firefox配置文件。但这似乎没有存储任何cookie,而且加载该配置文件也不起作用,我得到错误:

OpenQA.Selenium.WebDriverException: 'Failed to set preferences: Unable to read profile preferences file'
但这不是唯一的问题,我在复制的文件夹中没有看到任何cookie文件,只有extensions文件夹和user.js文件有一些首选项。以下是我使用的代码:

        internal static void TestCookies()
    {
        //initialize driver and profile
        FirefoxProfile profile = new FirefoxProfile();
        FirefoxDriver driver = new FirefoxDriver(profile);
        profile.DeleteAfterUse = false;
        //set a cookie
        driver.Navigate().GoToUrl("http://winware.org/en/cookie-permanent.php");
        driver.FindElementByXPath("html/body/form/table/tbody/tr[4]/td[2]/input").SendKeys("TestValue1");
        driver.FindElementByXPath("html/body/form/table/tbody/tr[7]/td[2]/button").Click();
        Thread.Sleep(3000);
        Console.WriteLine(driver.Manage().Cookies.AllCookies.Count);
        profile.SetPreference("accessibility.typeaheadfind.flashBar", false);
        profile.WriteToDisk();
        Console.WriteLine(profile.ProfileDirectory);
        //move the temp directory to a different location
        Directory.Move(profile.ProfileDirectory, @"C:\Data\FirefoxProfiles\customprofile\");
        driver.Close();

        //test if cookie is still there
        profile = new FirefoxProfile(@"C:\Data\FirefoxProfiles\customprofile\"); //crashes here OpenQA.Selenium.WebDriverException: 'Failed to set preferences: Unable to read profile preferences file'
        driver = new FirefoxDriver(profile);

        driver.Navigate().GoToUrl("http://winware.org/en/cookie-permanent.php");
        Thread.Sleep(3000);
        Console.WriteLine(driver.Manage().Cookies.GetCookieNamed("PermanentCookie").Value);

    }

有人知道如何在不手动管理Cookie的情况下执行此操作吗?

下次为什么不重新使用此配置文件?我尝试过,但没有效果,我描述了问题。当我尝试重新使用该配置文件时,它会崩溃并出现错误。我还检查了那个档案文件夹,那里似乎没有存储cookies。这是因为添加了与webdriver相关的东西。你能改用Chrome吗?在这种情况下,我会尝试使用chrome,但我仍然希望firefox有一个解决方案,如果没有,我会使用chrome,如果chrome可以工作的话。firefox需要大量的工作才能工作,然后它可能会工作,并在下一个版本中崩溃。所以我建议你使用Chrome为什么下次不重新使用这个配置文件呢?我试过了,但没用,我描述了这个问题。当我尝试重新使用该配置文件时,它会崩溃并出现错误。我还检查了那个档案文件夹,那里似乎没有存储cookies。这是因为添加了与webdriver相关的东西。你能改用Chrome吗?在这种情况下,我会尝试使用chrome,但我仍然希望firefox有一个解决方案,如果没有,我会使用chrome,如果chrome可以工作的话。firefox需要大量的工作才能工作,然后它可能会工作,并在下一个版本中崩溃。所以我建议你使用chrome