How get all cookies in the given url using javascript or selenium?

1.1k views Asked by At

Basically i want to access all cookies in a given url . For example i have attached image cookie

I need to access this data. is it possible to call firebug api to get cookie ? Is there any method from selenium ? I tried document.cookie . not working as expected .

2

There are 2 answers

0
Vaibs_Cool On

Try

function get_cookie ( cookie_name )
{
  // http://www.testsite.com/cookies.shtml
  var cookie_string = document.cookie ;
  if (cookie_string.length != 0) {
    var cookie_value = cookie_string.match ( '(^|;)[\s]*' + cookie_name + '=([^;]*)' );
    return decodeURIComponent ( cookie_value[2] ) ;
  }
  return '' ;
}

and then use this function to get cookie

for eg

cookieValue = get_cookie( "mycookies" );  //here mycookies is cookie name
0
Vikas Ojha On

You can get the cookies using Python and Selenium by using the following code -

driver = webdriver.Chrome(chromebinary)
driver.get(url)
cookies = driver.get_cookies()
for cookie in self.driver.get_cookies():
    print cookie['name'], cookie['value']