{"id":764,"date":"2023-02-26T16:14:34","date_gmt":"2023-02-26T08:14:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=764"},"modified":"2023-04-29T16:58:41","modified_gmt":"2023-04-29T08:58:41","slug":"python-selenium-cookie-operations","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-cookie-operations\/","title":{"rendered":"Python Selenium\u4e4bCookie\u64cd\u4f5c"},"content":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u6d4f\u89c8\u5668\u4e2dCookie\u662f\u5426\u6b63\u786e\uff0c\u56e0\u4e3a\u57fa\u4e8e\u771f\u5b9eCookie\u7684\u6d4b\u8bd5\u662f\u65e0\u6cd5\u901a\u8fc7\u767d\u76d2\u548c\u96c6\u6210\u6d4b\u8bd5\u8fdb\u884c\u7684\u3002WebDriver\u63d0\u4f9b\u4e86\u64cd\u4f5cCookie\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bfb\u53d6\u3001\u6dfb\u52a0\u548c\u5220\u9664Cookie\u4fe1\u606f\u3002<\/p>\n<p>WebDriver\u64cd\u4f5cCookie\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>get_cookies()\uff1a\u83b7\u5f97\u6240\u6709Cookie\u4fe1\u606f<\/li>\n<li>get_cookie(name)\uff1a\u8fd4\u56de\u5b57\u5178\u7684key\u4e3a\u201cname\u201d\u7684Cookie\u4fe1\u606f<\/li>\n<li>add_cookie(cookie_dict)\uff1a\u6dfb\u52a0Cookie\u3002\u201ccookie_dict\u201d\u6307\u5b57\u5178\u5bf9\u8c61\uff0c\u5fc5\u987b\u6709name \u548cvalue \u503c<\/li>\n<li>delete_cookie(name,optionsString)\uff1a\u5220\u9664Cookie\u4fe1\u606f\u3002\u201cname\u201d\u662f\u8981\u5220\u9664\u7684Cookie\u7684\u540d\u79f0\uff0c\u201coptionsString\u201d\u662f\u8be5Cookie\u7684\u9009\u9879\uff0c\u76ee\u524d\u652f\u6301\u7684\u9009\u9879\u5305\u62ec\u201c\u8def\u5f84\u201d\uff0c\u201c\u57df\u201d<\/li>\n<li>delete_all_cookies()\uff1a\u5220\u9664\u6240\u6709Cookie\u4fe1\u606f<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u901a\u8fc7<code>get_cookies()<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u6d4f\u89c8\u5668\u7684Cookie\u4fe1\u606f<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Firefox()\ndriver.get(&quot;http:\/\/www.youdao.com&quot;)\n\n# \u83b7\u5f97cookie\u4fe1\u606f\ncookie= driver.get_cookies()\n# \u5c06\u83b7\u5f97cookie\u7684\u4fe1\u606f\u6253\u5370\nprint(cookie)\n\ndriver.quit()<\/code><\/pre>\n<p>\u4ece\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cCookie\u6570\u636e\u662f\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u8fdb\u884c\u5b58\u653e\u7684\u3002\u77e5\u9053Cookie\u7684\u5b58\u653e\u5f62\u5f0f\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u6309\u7167\u8fd9\u79cd\u5f62\u5f0f\u5411\u6d4f\u89c8\u5668\u4e2d\u5199\u5165Cookie\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n\ndriver = webdriver.Firefox()\ndriver.get(&quot;http:\/\/www.youdao.com&quot;)\n\n# \u5411cookie\u7684name \u548cvalue\u4e2d\u6dfb\u52a0\u4f1a\u8bdd\u4fe1\u606f\ndriver.add_cookie({&#039;name&#039;: &#039;key-aaaaaaa&#039;, &#039;value&#039;: &#039;value-bbbbbb&#039;})\n\n# \u904d\u5386cookies\u4e2d\u7684name \u548cvalue\u4fe1\u606f\u5e76\u6253\u5370\uff0c\u5f53\u7136\u8fd8\u6709\u4e0a\u9762\u6dfb\u52a0\u7684\u4fe1\u606f\nfor cookie in driver.get_cookies():\n    print(&quot;%s -&gt; %s&quot; % (cookie[&#039;name&#039;], cookie[&#039;value&#039;]))\n\ndriver.quit()<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>======================== RESTART: =========================\nYOUDAO_MOBILE_ACCESS_TYPE -&gt; 1\n_PREF_ANONYUSER__MYTH -&gt; aGFzbG9nZ2VkPXRydWU=\nOUTFOX_SEARCH_USER_ID -&gt; -1056383866@218.18.158.112\nJSESSIONID -&gt; abc7qSE_SBGsVgnVLBvcu\nkey-aaaaaaa -&gt; value-bbbbbb<\/code><\/pre>\n<p>\u4ece\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u5230\uff0c\u6700\u540e\u4e00\u6761Cookie\u4fe1\u606f\u662f\u5728\u811a\u672c\u6267\u884c\u8fc7\u7a0b\u4e2d\u901a\u8fc7<code>add_cookie()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u6d4f\u89c8\u5668\u4e2dCookie\u662f\u5426\u6b63\u786e\uff0c\u56e0\u4e3a\u57fa\u4e8e\u771f\u5b9eCookie\u7684\u6d4b\u8bd5\u662f\u65e0\u6cd5\u901a\u8fc7\u767d\u76d2\u548c\u96c6\u6210\u6d4b\u8bd5\u8fdb\u884c\u7684\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221],"tags":[],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}