Selenium2+python automation 41-bypass verification code (add_cookie) Selenium2+python automation 40-cookie related operations

foreword

The verification code is a headache. For the processing of the verification code, don't think about the cracking method. This verification code is originally designed to prevent others from automatically logging in. If you can crack it, it means that your company's verification code is not high security level, then you need to improve the level.

For the verification code, either let the developer get a universal verification code in the test environment, such as: 1234, or try to bypass it as much as possible, such as the method of adding cookies introduced in this article.

1. Fiddler captures packets

1. As mentioned in the previous article, a logged-in cookie will be generated after logging in, so you only need to add this value directly to the cookies.

2. You can log in manually first, and then grab this cookie. Here you need to use the packet capture tool fiddler

3. First open the blog garden login interface, manually enter the account number and password (do not click the login button)

4. Open the fiddler package capture tool, and then click the blog garden login button

5. After the login is successful, check the cookie changes and find that there are two more sets of parameters. These two sets of parameters are what we want. Copy them out and it will be useful later.

 

2. Add cookie method: driver.add_cookie()

1. The parameter in the add_cookie(cookie_dict) method is cookie_dict, indicating that the parameter is a dictionary type.

2. Introduction to the official documentation of the source code:

add_cookie(self, cookie_dict)
Adds a cookie to your 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 can be seen from the official documentation, when adding a cookie, you can pass in the dictionary type. The left side of the equal sign is the name, and the left side of the equal sign is the value.

4. Write the two sets of data captured earlier (the parameters are not only name and value) as a dictionary type:

{'name':'.CNBlogsCookie','value':'2C3AE01E461B2D2F1572D02CB936D77A053089AA2xxxx...'}

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

 

3. Cookie composition structure

1. With the packet capture tool fidller, you can only see the name and value parameters of the cookie. In fact, the cookie has other parameters.

2. The cookie parameters are composed. The following parameters are obtained by me through get_cookie(name),

Refer to the previous article: Selenium2+python automates 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: the name of the cookie

value: the value corresponding to the cookie, dynamically generated

domain: server domain name

expiry: Cookie effective expiration date

path: The Path attribute defines which pages on the web server can obtain the cookies set by the server

httpOnly: Anti-script attack

secure: Mark this variable in the cookie, indicating that only when the communication protocol between the browser and the Web Server is an encrypted authentication protocol,

The browser submits the corresponding cookie to the server. Currently there is only one such protocol, which is HTTPS.

 

Fourth, add cookies

1. Two cookies need to be added here, one is .CNBlogsCookie and the other is .Cnblogs.AspNetCore.Cookies.

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

3. Refresh the page after adding the cookie, and the next moment is to witness the miracle.

 

5. Reference code:

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

driver = webdriver.Firefox()
driver.get("http://www.cnblogs.com/yoyoketang")

# # 添加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'/',driver.add_cookie(c1) # add 2 values      u'secure': False}
      u'httpOnly': True, 



driver.add_cookie(c2)
time.sleep(3)       
# Refresh the next page and witness the miracle
driver.refresh()  

A few things to note:

1. When logging in, check the next automatic login button.

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

3. This method is not suitable for all websites. Generally, it is suitable for those who remember the login status like Blog Park .

Guess you like

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