Selenium2+python Automation 40-cookie related operations

Source: Internet
Author: User

Objective

Although cookie-related operations are used less often in normal UI automation, they are occasionally used, such as login with a graphics verification code, you can bypass the authentication code method, add a cookie method to log in.

Login After login, also can be used as a post-condition to delete the cookie and then the next account login

I. Acquisition of Cookies:get_cookies ()

1. Methods of obtaining cookies directly by: Get_cookies ()

2. Start the browser first, get the cookies, print out the Found is empty: []

3. After opening the homepage of the blog, re-obtain the cookies, print out, there is a value

Second, after the login of the cookies

1. Log in to the blog Park first (login here with your own account and password bar)

2. Re-access cookies, found not the same as previously acquired

3. The main thing is to find this cookie and find that its name and value have changed, which is the difference between not logged in and logged in (compare the top and bottom two graphs)

{u ' name ': U '. Cnblogscookie ', U ' value ': U ' b7813eba142142ce88cc8c0b33b239f566xxxx '}

Third, get the name of the Cookie:driver.get_cookie (name) specified

1. Get cookies found there are multiple cookies, sometimes we only need one of them, put important, such as login cookie

2. Use Get_cookie (name) to specify the name value of the corresponding cookie, as in the blog Park:. Cnblogscookie

Iv. clear the specified Cookie:delete_cookie ()

1. In order to further verify that the previous step obtained is the login cookie, you can delete it to see what changes the page

2. After deleting this cookie, refresh the page and find that the login is invalid and become not logged in.

V. Clear All cookies:delete_all_cookies ()

1. After clearing all cookies, the login status also expires, and the cookies are blank []

Vi. Several methods of cookie operation

1.get_cookies (): Get All Cookies

2.driver.get_cookie (name): Gets the cookie for the specified name:

3. Clear the specified Cookie:delete_cookie ()

4.delete_all_cookies (): Clear All Cookies

5.add_cookie (cookie_dict): Adding the value of a cookie

(The fifth method can be used to bypass the verification code login, detailed in the next article)

Seven, reference code

# Coding:utf-8
From selenium import Webdriver
Import time

Driver = Webdriver. Firefox ()
# get cookies after launching your browser
Print driver.get_cookies ()
Driver.get ("http://www.cnblogs.com/yoyoketang/")
# Get cookies When you open the homepage
Print driver.get_cookies ()
# Get cookies After Login
url = "Https://passport.cnblogs.com/user/signin"
Driver.get (URL)
Driver.implicitly_wait (30)
driver.find_element_by_id ("Input1"). Send_keys (U "Shanghai-leisurely")
driver.find_element_by_id ("Input2"). Send_keys (U "xxx")
driver.find_element_by_id ("Signin"). Click ()
Time.sleep (3)
Print driver.get_cookies ()

# Gets the cookie for the specified name
Print Driver.get_cookie (name= ". Cnblogscookie ")

# Clears the cookie for the specified name
Driver.delete_cookie (name= ". Cnblogscookie ")
Print driver.get_cookies ()
# in order to verify that this cookie is logged in, you can refresh the page after deleting
Driver.refresh ()

# Clear All Cookies
Driver.delete_all_cookies ()
Print driver.get_cookies ()

Selenium2+python Automation 40-cookie related operations

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.