'How to send cookies with selenium webdriver?

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 operation?

Using Chrome and Firefox drivers, java language.



Solution 1:[1]

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);

Solution 2:[2]

driver.manage().addCookie();

manage()

The Options interface with Cookies

And Selenium's implementation of Cookie

Solution 3:[3]

In my case, the following code is working fine-

String token = tokenValue.substring(7);
    Cookie name = new Cookie("Token", token);
    driver.manage().addCookie(name);

Solution 4:[4]

Cookies failed to load onto some websites, so, the way I handled this problem was to create a user profile/data folder. My browser then behaves like a regular browser, you wouldn't have to manually store the cookies yourself.

JAVA

ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=USER_DATA_PATH");
driver = new ChromeDriver(options);

Python

chrome_options = Options()
chrome_options.add_argument(f"user-data-dir={USER_DATA_PATH}") 
driver = webdriver.Chrome(options=chrome_options)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cory Klein
Solution 2 Thai
Solution 3 Shivam Bharadwaj
Solution 4 Ini