Selenium2+python Automation 41-Bypass Verification Code (ADD_COOKIE)

Source: Internet
Author: User

Objective

Verification code This problem is more headache, for the Verification code processing, do not want to crack method, this verification code is to prevent others automated login. If you can decipher and explain your company's verification code, the security level is not high, then you need to improve the level.

For the verification code, either let development in the test environment to get a universal verification code, such as: 1234, or as far as possible around the past, such as the introduction of the method of adding cookies.

One, fiddler grab bag

1. As mentioned in the previous article, the login will generate a logged in state cookie, then only need to add this value directly into the cookie.

2. You can log in manually once, and then crawl this cookie, you need to use the Grab Kit tool fiddler

3. First open the Blog Park login screen, manually enter the account and password (do not point to the login button)

4. Open the Fiddler grab Bag tool, then click the Blog Park login button

5. After the successful login, then look at the cookie changes, found that two sets of parameters, many of these two sets of parameters is what we want, copy out, one will be useful

Second, add a cookie method: Driver.add_cookie ()

The 1.add_cookie (cookie_dict) method contains Cookie_dict, which indicates that the parameter is a dictionary type.

2. Official Source Document Description:

Add_cookie (self, cookie_dict)
Adds a cookie to your the current session.

: Args:
-Cookie_dict:a Dictionary object, with required keys-"name" and "value";
Optional Keys-"path", "Domain", "secure", "expiry"

Usage:
Driver.add_cookie ({' name ': ' foo ', ' value ': ' Bar '})
Driver.add_cookie ({' name ': ' foo ', ' value ': ' Bar ', ' path ': '/'})
Driver.add_cookie ({' name ': ' foo ', ' value ': ' Bar ', ' path ': '/', ' Secure ': True})

3. As you can see from the official documentation, the dictionary type is passed in when the cookie is added, the name to the left of the equals sign, and the value to the left of the equals sign.

4. Put the two sets of data (parameters not only name and value) that were caught in front of it, and write the dictionary type:

{' name ': '. Cnblogscookie ', ' value ': ' 2c3ae01e461b2d2f1572d02cb936d77a053089aa2xxxx ... '}

{' name ': '. Cnblogs.AspNetCore.Cookies ', ' value ': ' Cfdj8mmb5oberd5fqtiqlkzzig4hkz_zxxx ... '}

Third, the structure of the cookie composition

1. Using the grab kit Fidller can only see the cookie name and value two parameters, in fact, the cookie has other parameters

The 2.cookie parameter consists of the following parameters that I obtained through Get_cookie (name),

Refer to Previous article: Selenium2+python Automation 40-cookie related operations

Cookie ={u ' domain ': U '. cnblogs.com ',
U ' name ': U '. Cnblogscookie ',
U ' value ': U ' xxxx ',
U ' expiry ': 1491887887,
U ' path ': U '/',
U ' httponly ': True,
U ' secure ': False}

Name of the Name:cookie

Value:cookie the corresponding value, the dynamically generated

Domain: Server domain name

Expiry:cookie Effective Termination Date

The Path:path property defines which paths on the Web server are under which pages can obtain server-set cookies

HttpOnly: Anti-scripting attack

Secure: Mark the variable in a cookie to indicate that only if the communication protocol between the browser and the Web server is a cryptographic authentication protocol,

The browser submits the appropriate cookie to the server. There is currently only one such protocol, that is, HTTPS.

Iv. Adding cookies

1. You need to add two cookies here, one is. Cnblogscookie, the other one is. Cnblogs.AspNetCore.Cookies.

2. The page I opened here is the homepage of the blog: Http://www.cnblogs.com/yoyoketang, which did not enter the login page.

3. After adding a cookie, refresh the page, and the next is the time to witness the miracle.

Five, reference code:

# Coding:utf-8
From Selenium import Webdriver
Import Time

Driver = Webdriver. Firefox ()
driver.get ("Http://www.cnblogs.com/yoyoketang")

# # Add a cookie
C1 = {u ' domain ': U '. cnblogs.com ',
u ' name ': U '. Cnblogscookie ',
u ' value ': U ' xxxx ',
u ' expiry ': 1491887887,
u ' path ': U '/',
u ' httponly ': True,
u ' secure ': False}

C2 = {u ' domain ': U '. cnblogs.com ',
u ' name ': U '. Cnblogs.AspNetCore.Cookies ',
u ' value ': U ' xxxx ',
u ' expiry ': 1491887887,
u ' path ': U '/',
u ' httponly ': True,
u ' secure ': False}

Driver.add_cookie (C1) # add 2 values
Driver.add_cookie (C2)
Time.sleep (3)
# Refresh the next page and witness the miracle.
Driver.refresh ()

There are a few points to note:

1. Check the next automatic login button when you log in.

2.add_cookie () only adds name and value, and the login for the blog Park is unsuccessful.

3. This method is not suitable for all sites, generally like the blog park This remember the login status will be appropriate .

Selenium2+python Automation 41-Bypass Verification Code (ADD_COOKIE)

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.