我将Chrome和chromewebdriver更新到了77版。此后,当我unable to set cookie
时出现错误(addCookie
)。
System.setProperty("cookie", "auth=ok,path=/");
int indexValue = cookieStr.indexOf('=');
int indexPath = cookieStr.indexOf(",path=");
String cookieName = cookieStr.substring(0, indexValue);
String cookieValue = cookieStr.substring(indexValue + 1, indexPath);
String cookieDomain = new URI("http://localhost").getHost().replaceAll("self.", "");
String cookiePath = cookieStr.substring(indexPath + 6);
Cookie authCookie= new Cookie.Builder(cookieName, cookieValue).domain(cookieDomain).path(cookiePath).expiresOn(new SimpleDateFormat("dd/MM/yyyy").parse("31/12/2020")).build();
System.out.println("A");
driver.navigate().to("http://localhost:8000/unprotected");
System.out.println("B");
driver.get("http://localhost:8000/404");
System.out.println("C");
System.out.println("[" + driver.getPageSource()+"]");
Options b = a.manage();
System.out.println("Domain: " + cookie.getDomain());
System.out.println("Name: " + cookie.getName());
System.out.println("Path: " + cookie.getPath());
System.out.println("Value: " + cookie.getValue());
System.out.println("Expiry: " + cookie.getExpiry());
b.addCookie(cookie); <= KO
System.out.println("D");
控制台:
A
B
C
[<html><head></head><body><h1>404 Not Found</h1>No context found for request</body></html>]
Domain: localhost
Name: auth
Path: /
Value: ok
Expiry: Thu Dec 31 00:00:00 CET 2020
错误:
org.openqa.selenium.UnableToSetCookieException: unable to set cookie
(Session info: chrome=77.0.3865.75)
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'xxxxxxxxx', ip: '192.168.0.111', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.75, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\JFTS8586\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:58344}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: accept}
Session ID: 82305922f96631ac61d95a737263cb64]
完整的代码在线上 here(在一类中),如果您想在本地计算机上重现此问题。
铬小组here的更多信息。
答案 0 :(得分:4)
您可以尝试使用127.0.0.1
而不是loclahost
。
Chrome不再支持在本地主机上设置域Cookie。如果您从Cookie设置中删除了域,则域应该可以正常工作。
更多详细信息:
当前的WebDriver标准尚不清楚应如何处理cookie域。 ChromeDriver的实现尝试遵循https://tools.ietf.org/html/rfc6265#section-5.3。根据cookie存储模型的项目6,指定cookie域将导致域cookie,而未指定域将导致仅主机cookie。但是,为localhost创建域cookie实际上没有任何意义,因为localhost不遵循域命名约定。 Chrome过去对此宽容,但Chrome 77现在具有拒绝为本地主机创建域cookie的代码,因此不再允许为本地cookie指定域。