'Capybara: How to set cookie before navigation / loading page
How can I set a cookie before navigation with Capybara? The following code raises an exception:
page.driver.browser.manage.add_cookie(
name: "name",
value: "value",
path: "/",
)
Selenium::WebDriver::Error::InvalidCookieDomainError:
invalid cookie domain
(Session info: headless chrome=98.0.4758.109)
Solution 1:[1]
Recent versions of Selenium Webdriver support sending Chrome DevTools Protocol (CDP) commands from the driver (this is possible with other drivers like Cuprite too).
Turns out the CDP Network.setCookie command allows one to specify a domain – allowing you to set cookies before navigating to a site.
Here's a Capybara + Selenium Webdriver example:
page.driver.browser.execute_cdp(
"Network.setCookie",
domain: "example.com",
name: "name",
value: "value",
path: "/",
)
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 | odlp |
