【问题标题】:Is there a way to clear session data to ensure a clean session is initiated each time?有没有办法清除会话数据以确保每次都启动干净的会话?
【发布时间】:2017-12-06 11:29:38
【问题描述】:

对于 IE,您将使用如下功能:

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

并可能结合

 driver.manage().deleteAllCookies(); 

如何使用 Chrome 和 ChromeDriver 实现这一点?

【问题讨论】:

  • Chrome 使用随机创建的配置文件开始每个会话,没有任何 cookie 或缓存。默认情况下,它会启动一个干净的会话,直到您更改它为止。

标签: java selenium-webdriver


【解决方案1】:

当我们使用 Internet Explorer Driver 时,我们使用字段 IE_ENSURE_CLEAN_SESSION

IE_ENSURE_CLEAN_SESSION

根据JavaDocs IE_ENSURE_CLEAN_SESSION是定义在IEDriverServer启动Internet Explorer之前是否清理浏览器缓存的能力,配置如下:

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);

现在让我们看看WebDriver Spec之后的GeckoDriver

GeckoDriver / moz:profile / rust_mozprofile

如果您仔细查看 geckodriver 日志,您会发现每次 geckodriver 都被称为新的 moz:profile 被排除在外,rust_mozprofile 的详细信息出现在以下行中:

Marionette  CONFIG  Matched capabilities: {"browserName":"firefox","browserVersion":"56.0","platformName":"windows_nt","platformVersion":"6.2","pageLoadStrategy":"normal","acceptInsecureCerts":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"rotatable":false,"specificationLevel":0,"moz:processID":5848,"moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d","moz:accessibilityChecks":false,"moz:headless":false}

此日志清楚地表明 Marionette 挖出了一个新的 "moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d",此配置由 WebDriver 实例处理,即 GeckoDriver

您可以在Is it Firefox or Geckodriver, which creates “rust_mozprofile” directory讨论中找到关于moz:profile的更详细讨论。


ChromeDriver

ChromeDriver 遵循同一个 WebDriver Spec 确实遵守(将遵守)同一个套件。


如果您使用任何存储的 FirefoxProfileChromeProfileWebDriver 将选择现有的配置文件,其中 Stored Browser Configurations 被拾取以供重复使用。


driver.manage().deleteAllCookies();

无论新的/现有的 FirefoxProfileChromeProfile 如果您添加以下行:

driver.manage().deleteAllCookies(); 

只有 cookie 被删除才能恢复以支持 Active Browser Session

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多