首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我无法为在无头模式下运行的ChromeDriver设置cookies

我无法为在无头模式下运行的ChromeDriver设置cookies
EN

Stack Overflow用户
提问于 2019-06-17 22:53:09
回答 2查看 3K关注 0票数 1

当我在无头模式下使用ChromeDriver运行selenium测试时,addCookie方法调用会导致异常;所有测试都在正常模式下工作

我的Chrome浏览器版本是75.0.3770.90,ChromeDriver版本是75.0.3770.8。操作系统为Mac OS X 10.14.5

我得到的例外是:

代码语言:javascript
运行
复制
    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)

导致问题的代码是:

代码语言:javascript
运行
复制
    driver.get(initialPageUrl);    
    Cookie cookie = new Cookie(cookieName, encode(cookieValue, "UTF-8"), "localhost","/", null);
    driver.manage().addCookie(cookie);
EN

Stack Overflow用户

发布于 2019-07-04 18:57:52

它在无头模式下看起来,你必须在页面url中有协议,而在正常模式下,web驱动程序缺省为http。因此,一旦我们将"localhost:8080/path“替换为"http://localhost:8080/path”,它就可以工作了。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56633840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档