Selenium Webdriver getting a cookie value

别说谁变了你拦得住时间么 提交于 2019-12-30 07:23:47

问题


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?


回答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



回答2:


This worked for me:

Cookie cookie= driver.manage().getCookieNamed("sitename.session");  
String cookieVal= cookie.getValue();



回答3:


    Set<Cookie> cook =  driver.manage().getCookies();
    for(Cookie cooks : cook) 
    {

    System.out.println(cooks.getName());
    }

    Cookie t = driver.manage().getCookieNamed("_gid");
    if(t!=null){
    String s1 = t.getValue();
    System.out.println("The Cookie value is : " + s1);
               }


来源:https://stackoverflow.com/questions/17721862/selenium-webdriver-getting-a-cookie-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!