Selenium-webdriver-cookie operation

Source: Internet
Author: User

Webdriver provides a way to manipulate cookies to read, add, and delete cookie information.

Webdriver How to manipulate cookies:

    • Get_cookies (): Get all cookie information.

    • Get_cookie (name): Returns the cookie information for the dictionary key "name".

    • Add_cookie (cookie_dict): Add a cookie. "Cookie_dict" refers to the Dictionary object, which must have the name and value values.

    • Delete_cookie (name,optionsstring): Delete cookie information. "Name" is the name of the cookie to be deleted, and "optionsstring" is the option for the cookie, and the options currently supported include "path", "Domain".

    • Delete_all_cookies (): Delete all cookie information.

The following is a get_cookies () to obtain cookie information for the current browser.

From selenium import webdriverdriver = Webdriver. Chrome () driver.get ("http://www.youdao.com") # get cookie Information cookie = driver.get_cookies () print (cookie)        Driver.quit ()

Execution Result:

[{' domain ': ' www.youdao.com ', ' httponly ': false, ' name ': ' ___rl__test__cookies ', ' path ': '/', ' secure ': false, ' value ': ' 1526219954928 '}, {' domain ': '. youdao.com ', ' expiry ': 1589291954, ' httponly ': False, ' name ': ' Outfox_search_user_id_ Ncoo ', ' path ': '/', ' secure ': False, ' value ': ' 900350754.7866102 '}]

As can be seen from the execution result, cookie data is stored in the form of a dictionary.

To add a cookie action:

From selenium import webdriverdriver = Webdriver. Chrome () driver.get ("http://www.youdao.com") # adds new information to the cookie Driver.add_cookie ({' name ': ' key-aaaaa ', ' value ': ' VALUE-BBBBB '}) # traversal cookie information for cookie in Driver.get_cookies ():    print ("%s"% (cookie[' name '), cookie[' value ' ])) Driver.quit ()

Execution Result:

outfox_search_user_id, [email protected]
DICT_UGC-be3af0da19b5c5e6aa4e17bd8d90b28a|
Jsessionid-ABCL3HQPWO42I51W90ZNW
___rl__test__cookies-1526220445878
KEY-AAAAA-VALUE-BBBBB
Outfox_search_user_id_ncoo-828629148.5563225

Article excerpt from: http://www.testclass.net/selenium_python/cookie/

Selenium-webdriver-cookie Action

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.