有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建组织。openqa。硒。IsSecurity标志为false的Cookie

创建cookie并定义我希望IsSecurity字段为false时:

 driver.manage().addCookie(new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.getExpiry(), false));

这是Selenium,实际上WebDriver将参数设置为true:

driver.manage().getCookies().forEach(cookie -> {
     System.out.println("Adding cookie isSecure: " + cookie.isSecure());
}); //prints Added cookie isSecure: true

我使用selenium版本:

 <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>

我不知道为什么会这样。 有什么解决办法吗


共 (1) 个答案

  1. # 1 楼答案

    你的代码将在Firefox中运行。看起来问题只在于chromedriver/chrome

    作为一种解决方法,请使用Firefox

    解释:

    用于添加cookie的Java绑定代码

    public Cookie(String name, String value, String domain, String path, Date expiry,
          boolean isSecure, boolean isHttpOnly)
    
    
    driver.manage().addCookie(
                        new Cookie("test", "test", "google.com", "/", null, false,false));
    

    对于以上代码,Webdriver正在向chromedriver服务器发送正确的值

    [1566377805.242][INFO]: [70c91dc21b299384c804d153e40a6b0d] COMMAND AddCookie {
       "cookie": {
          "domain": "google.com",
          "httpOnly": false,
          "name": "test",
          "path": "/",
          "secure": false,
          "value": "test"
       }
    }
    

    甚至Chrome Devtools也在呼叫网络。用正确的数据设置cookie

    DevTools WebSocket Command: Network.setCookie (id=26) BB77CD380D314C209C8F2F8AE97C504D {
       "domain": ".google.com",
       "httpOnly": false,
       "name": "test",
       "path": "/",
       "secure": false,
       "url": "https://www.google.com/?gws_rd=ssl",
       "value": "test"
    }
    

    对于get来说,它的返回是“安全的”:true

    [1566377805.253][DEBUG]: DevTools WebSocket Response: Network.getCookies (id=30) BB77CD380D314C209C8F2F8AE97C504D {
    "cookies": [ {
    {
          "domain": ".google.com",
          "expires": -1,
          "httpOnly": false,
          "name": "test",
          "path": "/",
          "secure": true,
          "session": true,
          "size": 8,
          "value": "test"
       }]
    

    从上面的日志来看:chromedriver/chrome似乎有问题,而不是Selenium Webdriver JAVA绑定

    对于Firefox,它可以正常工作

    WebDriver发送post请求

    DEBUG   -> POST /session/8600dbc1-e2cd-449d-ad0d-fc5261e37266/cookie {
      "cookie": {
        "domain": "google.com",
        "httpOnly": false,
        "name": "test",
        "path": "\u002f",
        "secure": false,
        "value": "test"
      }
    }
    

    木偶也能发送正确的数据

    Marionette  DEBUG   0 -> [0,3,"WebDriver:AddCookie",{"cookie":{"domain":"google.com","httpOnly":false,"name":"test","path":"/","secure":false,"value":"test"}}]
    

    接收到正确的值“安全”:false

    Marionette  DEBUG   0 -> [0,4,"WebDriver:GetCookies",{}]
    1566378528552   Marionette  DEBUG   0 <- [1,4,null,[{"name":"test","value":"test","path":"/","domain":".google.com","secure":false,"httpOnly":false}]]
    

    Firefox/Geckodriver中没有问题