Python Selenium Cookie Bypass Verification Code Implementation Login Sample Code _

Source: Internet
Author: User
This article mainly describes the Python Selenium Cookie Bypass Verification Code Implementation login sample code, now share to everyone, the need for friends can refer to

Previously, the method of using cookies to bypass verification codes for login was described. This is not redundant and will add analysis and another way to implement login.

1. Introduction of Ideas

1.1, directly see the code, with detailed comments in the description

# filename:wm_cookie_login.py# author:adil# datetime:2018/3/20 19:47# software:pycharmfrom Selenium Import Webdri Verimport timeurl = ' https://system.address ' def login (): "First define a normal login method, get pre-logon and post-login cookie ' ' Driver = webdriver. Chrome () driver.get (URL) driver.maximize_window () Cookiebefore = Driver.get_cookies () # Print pre-logon cookie print (COOKIEBEFO RE) time.sleep (2) driver.find_element_by_id ("New-username"). Clear () driver.find_element_by_id ("New-username"). Send_keys ("username") driver.implicitly_wait (5) driver.find_element_by_id ("New-password"). Clear () Driver.find_  element_by_id ("New-password"). Send_keys ("password") driver.find_element_by_id (' Home-right-login '). Click () Driver.implicitly_wait (5) # Plus one hibernate, so the cookie is the cookie that is logged in, otherwise it may print the cookie Time.sleep (5) Print before login ") Cookiesafter = Driver.get_cookies () print (" Cookiesafter: ") print (cookiesafter) # cookie is stored in the list, which is the Dict # comparison found after login  The cookie is 4 more dict than before the login. # The following code is 1, 4, 7, 8 len1 = Len (cookiesafter) print ("Len:%d "%len1) cookie1 = cookiesafter[0] Cookie2 = cookiesafter[3] cookie3 = cookiesafter[-2] cookie4 = cookiesafter[-1]  Print ("cookie1:%s"%cookie1) print ("cookie2:%s"%cookie2) print ("cookie3:%s"%cookie3) print ("cookie4:%s"%cookie4) Driver.quit () # will get these four cookies as parameters, passed to the function using a cookie login, as follows Cookielogin (COOKIE1,COOKIE2,COOKIE3,COOKIE4) def Cookielogin (COOKIE1,COOKIE2,COOKIE3,COOKIE4): Print ("+++++++++++++++++++++++++") print ("Cookielogin") print ("cookie2:%s"% COOKIE2) Print ("cookie4:%s"% cookie4) Driver = Webdriver. Chrome () Driver.maximize_window () # Clear Cookies driver.delete_all_cookies () time.sleep (3) driver.get (URL) # Add an interview after opening the browser After asking the address, add a cookie Driver.add_cookie (cookie1) Driver.add_cookie (cookie2) Driver.add_cookie (cookie3) Driver.add_cookie ( COOKIE4) Print ("Cookies") # Prints a cookie and compares it to a cookie that is normally logged on, print (Driver.get_cookies ()) Time.sleep (5) # Refresh the page to see that it is already a login  This completes the login using the cookie. Driver.refresh () Time.sleep (5) driver.quit () if __name__ = = "__main__": Login () 

1.2. Code Introduction

, you can view the pre-logon and post-login cookies, copy them, and compare them

, compare Yes, four extra cookies after login

View the location of the extra cookie, which is the 1, 4, 7, 8g elements of the list, so take it out as a parameter to the cookie login function.

Note: This example describes a no-code sign-in operation, but the idea is the same. And this demo, just to introduce the use of cookies to login ideas, specific project applications, this is very inconvenient.

2, bypass the verification code login combat

Next, introduce the login containing the verification code, of course, the idea of cookie processing is the same as the basic

Here are a few things to add:

A, first use the verification code to correctly log in and save the pre-login, after the cookie, compare and analyze cookies, filter useful cookies

b, write the cookie to the Yaml file, so that it can be used directly when the cookie is logged in, without having to log on as usual, as described above.

C. When you log in using a cookie, read the corresponding cookie from the Yaml file. Note: YAML file operations are described in Python Yaml Learning, which provides a detailed description of Yaml's read and write operations.

Note: Here is the manual entry of the verification code to get the cookie correctly after login. Other ways to get cookies in real-world applications

such as: 1, before the blog Park login instance: Python-cookie Bypass Verification code login using fiddler, view cookies

2. Use the browser to view cookies such as, with the help of chrome plugin such as, export cookies for analysis.

On the line of thought, the code is as follows:

2.1, normal login to obtain a valid cookie

# filename:getlogincookie.py# author:adil# datetime:2018/3/20 21:43# software:pycharmimport yaml,time,osfrom Sele Nium Import webdriverurl = ' https://system.address ' driver = webdriver. Chrome () driver.get (URL) driver.maximize_window () Time.sleep (2) driver.find_element_by_id ("username"). Clear () DRIVER.FIND_ELEMENT_BY_ID ("username"). Send_keys ("username") driver.implicitly_wait (5) driver.find_element_by_id ( "Password"). Clear () driver.find_element_by_id ("password"). Send_keys ("password") print ("Please Enter Verification Code:") # Manually enter the CAPTCHA security _code = input () time.sleep (1) driver.find_element_by_id ("Security_code"). Send_keys (Security_code) time.sleep (1) driver.find_element_by_id (' sign_btn '). Click () driver.implicitly_wait (5) # Add a hibernate so that the cookie is the cookie that is logged in, Otherwise it may be printed cookietime.sleep (5) Cookiesafter = Driver.get_cookies () len1 = Len (cookiesafter) # already know the number of cookies required, This requires a 3rd cookie, so select the cookie subscript 2cookie1 = cookiesafter[2]# Get the path of the current file Filenamepath = Os.path.split (Os.path.realpath (__ file__)) [0]# stitching Config.yaml File absolute path Yamlpath = Os.patH.join (Filenamepath, ' Config.yaml ') # to overwrite write Open file FW = open (Yamlpath, ' W ', encoding= ' Utf-8 ') # Build data = {"Cookie1": cookie1 The}# mount is written to the Yaml file. Yaml.dump (DATA,FW) driver.quit ()

2.2. Read the cookie configuration file and use the cookie to log into the system

# filename:stlusecookielogin.py# Author  : adil# datetime:2018/3/20 21:48# software:pycharmfrom Selenium Import W Ebdriverimport time,yaml,osurl = ' https://system.address ' driver = webdriver. Chrome () Driver.maximize_window () driver.delete_all_cookies () Time.sleep (3) driver.get (URL) filenamepath = Os.path.split (Os.path.realpath (__file__)) [0]yamlpath = Os.path.join (Filenamepath, ' Config.yaml ') # read Yaml file F = open ( Yamlpath, ' R ', encoding= ' utf-8 ') cont = F.read () conf = Yaml.load (cont) # Read Cookie value cookie1 = Conf.get ("Cookie1") # Add Cookiedriver.add_cookie (cookie1) print ("Cookies") Print ("Driver.get_cookies ()) Time.sleep (5) # here re-get the address, because some systems, Not logged in status, the link will jump, here is, login status, to correctly open the specified URL, so here to specify the URL again. Driver.get (URL) # refresh view login Status Driver.refresh () Time.sleep (5) Driver.quit ()


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.