Selenium Webdriver getting a cookie value
15,158
Solution 1
The methods for working with cookies are defined in the Selenium::WebDriver::Options - see the API docs.
To access these cookie methods, you need to call the manage
method for the driver:
@browser.manage
To get a cookie based on its name, you need to do:
@browser.manage.cookie_named("configsession")
Note that cookie_named
returns a single cookie that matches. The cookies values are a hash. Therefore, you can get values of the cookie by doing:
cookie = @browser.manage.cookie_named("configsession")
cookie[:name]
#=> "configsession"
If you want to get the name of all the cookies on the page, use the all_cookies
method:
driver.manage.all_cookies.each do |cookie|
puts cookie[:name]
end
Solution 2
This worked for me:
Cookie cookie= driver.manage().getCookieNamed("sitename.session");
String cookieVal= cookie.getValue();
Author by
user1875703
Updated on June 04, 2022Comments
-
user1875703 almost 2 years
I am trying to get a cookie value but keep getting an error of
<Selenium::WebDriver::Driver:0x13a0e0e8 browser=:firefox>
I am calling
@browser.cookie_named("configsession").each do |cookie| puts cookie[:name]
is there something I i'm doing wrong?