简体   繁体   English

如何使用javascript或selenium获取给定URL中的所有cookie?

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

Basically i want to access all cookies in a given url . 基本上我想访问给定URL中的所有cookie。 For example i have attached image 例如我附有图片 曲奇饼

I need to access this data. 我需要访问此数据。 is it possible to call firebug api to get cookie ? 是否可以调用firebug api获取cookie? Is there any method from selenium ? 硒有什么方法吗? I tried document.cookie . 我尝试了document.cookie not working as expected . 不能按预期工作。

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 然后使用此功能获取Cookie

for eg 例如

cookieValue = get_cookie( "mycookies" );  //here mycookies is cookie name

You can get the cookies using Python and Selenium by using the following code - 您可以使用以下代码使用Python和Selenium获取Cookie:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM