Selenium2+python automates 40-cookie related operations

foreword

Although cookie-related operations are rarely used in normal UI automation, they are occasionally used. For example, if there is a graphical verification code for login, you can log in by bypassing the verification code and adding a cookie method.

After logging in, when changing accounts to log in, it can also be used as a post-condition to delete cookies and then log in with the next account.

1. Get cookies: get_cookies()

1. Use the method to get cookies directly: get_cookies()

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

3. After opening the homepage of the blog, re-acquire cookies, print them out, and they will be worthwhile

2. Cookies after login

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

2. Re-acquire cookies and find that they are different from those obtained before

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 upper and lower pictures)

{u'name': u'.CNBlogsCookie', u'value': u'B7813EBA142142CE88CC8C0B33B239F566xxxx'}

3. Get the cookie with the specified name: driver.get_cookie(name)

1. Get cookies and find that there are multiple cookies in it. Sometimes we only need one of them, and bring out the important ones, such as the login cookie

2. Use get_cookie(name) here to specify the name value of the corresponding cookie, such as blog garden: .CNBlogsCookie

 

Fourth, clear the specified cookie: delete_cookie()

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

2. After deleting this cookie, refresh the page and find that the login just now has expired, and it has become a non-login state.

5. Clear all cookies: delete_all_cookies()

1. After clearing all cookies, the login status is also invalid, the cookies are empty[]

6. Several methods of cookie operation

1.get_cookies(): Get all cookies

2.driver.get_cookie(name): Get the cookie with the specified name:

3. Clear the specified cookie: delete_cookie()

4.delete_all_cookies(): Clear all cookies

5.add_cookie(cookie_dict): add the value of the cookie

(The fifth method can be used to bypass the verification code login, which will be described in detail in the next article)

7. Reference code

# coding:utf-8
from selenium import webdriver
import time

driver = webdriver.Firefox()
# Get cookies after starting the browser
print driver.get_cookies()
driver.get("http://www.cnblogs.com/yoyoketang/" )
# Get cookies after opening the home page
print driver.get_cookies()
# Get cookies after logging in
url = "https://passport.cnblogs.com/user/signin"
driver.get(url)
driver.implicitly_wait(30)
driver.find_element_by_id ("input1").send_keys(u"Shanghai-Yoyou")
driver.find_element_by_id("input2").send_keys(u"xxx")
driver.find_element_by_id("signin").click()
time.sleep(3)
print driver.get_cookies()

# Get the cookie with the specified name
print driver.get_cookie(name=".CNBlogsCookie ")

# Clear the cookie with the specified name
driver.delete_cookie(name=".CNBlogsCookie")
print driver.get_cookies()
# To verify that this cookie is logged in, you can delete it and refresh the page
driver.refresh()

# Clear all cookies
driver.delete_all_cookies ()
print driver.get_cookies()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325456629&siteId=291194637