当我在 headless 模式下使用 ChromeDriver 运行 Selenium 测试时,addCookie 方法调用会导致异常;测试全部在正常模式下进行
我的 Chrome 浏览器版本是 75.0.3770.90,ChromeDriver 版本是 75.0.3770.8。操作系统是Mac OS X 10.14.5
我得到的异常(exception)是:
org.openqa.selenium.UnableToSetCookieException: unable to set cookie
(Session info: headless chrome=75.0.3770.90)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'A0353.local', ip: 'fe80:0:0:0:84c:de05:352a:4bcc%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.5', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.90, chrome: {chromedriverVersion: 75.0.3770.8 (681f24ea911fe7..., userDataDir: /var/folders/nc/gqkjhl8920j...}, goog:chromeOptions: {debuggerAddress: localhost:63795}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: db1528ca16e944bb6596563337e1ba40
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions.addCookie(RemoteWebDriver.java:682)
at com.anatwine.testing.common.selenium.PageObject.addCookie(PageObject.java:187)
at com.anatwine.testing.component.page.landingpage.LandingPage.addUsernameAndRolesCookies(LandingPage.java:54)
at com.anatwine.testing.component.steps.portallandingpage.RoleBasedAuthorisationSteps.lambda$new$347(RoleBasedAuthorisationSteps.java:32)
at com.anatwine.testing.component.steps.portallandingpage.RoleBasedAuthorisationSteps$$Lambda$494/785340693.accept(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at cucumber.runtime.Utils$1.call(Utils.java:26)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:20)
at cucumber.runtime.java8.Java8StepDefinition.execute(Java8StepDefinition.java:112)
at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
at cucumber.runner.TestStep.executeStep(TestStep.java:63)
at cucumber.runner.TestStep.run(TestStep.java:49)
at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
at cucumber.runner.TestCase.run(TestCase.java:44)
at cucumber.runner.Runner.runPickle(Runner.java:40)
at cucumber.runtime.Runtime$1.run(Runtime.java:84)
at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:220)
at cucumber.runtime.Runtime.run(Runtime.java:81)
at cucumber.api.cli.Main.run(Main.java:26)
at cucumber.api.cli.Main.main(Main.java:8)
at ✽.A user has 'ROLE_LANDING,ROLE_BRAND_ORDERSIM' roles(feature/component/portal-landingpage/PortalLandingPageRoleAuthorisations.feature:12)
导致问题的代码是:
driver.get(initialPageUrl);
Cookie cookie = new Cookie(cookieName, encode(cookieValue, "UTF-8"), "localhost","/", null);
driver.manage().addCookie(cookie);
最佳答案
您可能在导航到该网站之前添加了 cookie。您应该首先导航到您的网址
driver.get("yourWebPage");
并且仅在此之后添加cookie
driver.manage().addCookie(cookie);
为了更好地分析出了什么问题,如果上述方法没有帮助,您必须添加更多的程序代码。
也可能是 this 的重复项.
关于java - 我无法为在 headless 模式下运行的 ChromeDriver 设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56633840/