对于 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
orChromeProfile
,WebDriver
will pick up the existing profile where theStored 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/