java - 有没有办法清除 session 数据以确保每次都启动一个干净的 session ?

标签 java selenium-webdriver

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

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

并可能结合

 driver.manage().deleteAllCookies(); 

这如何使用 Chrome 和 ChromeDriver 实现?

最佳答案

当我们与一起工作时Internet Explorer Driver 我们使用字段 IE_ENSURE_CLEAN_SESSION

IE_ENSURE_CLEAN_SESSION

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

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

现在让我们看看 GeckoDriver 跟在 WebDriver Spec 之后.

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 .

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


ChromeDriver

ChromeDriver 跟随相同的 WebDriver Spec 确实遵守(将遵守)同一套房。


Incase you are using any stored FirefoxProfile or ChromeProfile, WebDriver will pick up the existing profile where the Stored Browser Configurations are picked up for reuse.


driver.manage().deleteAllCookies();

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

driver.manage().deleteAllCookies(); 

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

关于java - 有没有办法清除 session 数据以确保每次都启动一个干净的 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673232/

相关文章:

java - JSON-B 将日期字符串反序列化为 joda DateTime 而不更改模型类

java - 将 csv 打开到输入流中

java - 是否有更优雅的方法来从流中选择 max 或 min 方法?

java - 需要通过 Retrofit 连接到 Google App Engine 的帮助

python-2.7 - Selenium Python 测试批处理文件未在任务计划程序中运行,浏览器未打开。批处理文件在没有任务计划程序的情况下运行正常

selenium - 如何使用带有 firefox 17.0.1 的 Webdriver 在 Mac 上以 headless 模式运行 selenium 测试

java - 如何在 Java 中安装 httpclient 和其他库以便在 Eclipse 中使用它们?

java - PhantomJS 的 Java 等价物是什么?

python - 无法让我的脚本向右滑动按钮

javascript - 带有 javascript 单击的相对 xpath