How to send cookies with selenium webdriver?

后端 未结 4 1544
星月不相逢
星月不相逢 2020-11-28 10:06

Every time when I run my test first step is log in and than I get to desire page. If run this test often log in operation takes a lot of time.

How can I pass log in

相关标签:
4条回答
  • 2020-11-28 10:22
    driver.manage().addCookie();
    

    manage()

    The Options interface with Cookies

    And Selenium's implementation of Cookie

    0 讨论(0)
  • 2020-11-28 10:25

    Create cookies using the Java API as follows:

    Cookie ck = new Cookie("name", "value");
    driver.manage().addCookie(ck);
    

    Create cookies using the Python API as follows:

    driver.add_cookie({'name': 'foo', 'value': 'bar'})
    
    0 讨论(0)
  • 2020-11-28 10:30

    In my case, the following code is working fine-

    String token = tokenValue.substring(7);
        Cookie name = new Cookie("Token", token);
        driver.manage().addCookie(name);
    
    0 讨论(0)
  • 2020-11-28 10:37

    For those that need to set more detailed information on Cookie besides name and value you can use:

    Cookie cookie = new Cookie.Builder("name", "value")
        .domain(".mydomain.com")
        .expiresOn(new Date(2015, 10, 28))
        .isHttpOnly(true)
        .isSecure(false)
        .path("/mypath")
        .build();
    
    driver.manage().addCookie(cookie);
    
    0 讨论(0)
提交回复
热议问题