Python Selenium Cookie Bypass Verification Code implementation login

Source: Internet
Author: User

Python Selenium Cookie Bypass Verification Code implementation login

Previously introduced the blog park through the cookie Bypass verification code to implement the method of login. 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:pycharm fromSeleniumImportWebdriverImportTimeurl='https://system.address'deflogin ():" "define a normal login method to get pre-logon and post-logon cookies" "Driver=Webdriver. Chrome () driver.get (URL) driver.maximize_window () Cookiebefore=driver.get_cookies ()#Print pre-logon cookies    Print(Cookiebefore) 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)    #Add a hibernation, so that the cookie is the login cookie, otherwise it may be printed or pre-logon cookieTime.sleep (5)    Print("after login! ") Cookiesafter=driver.get_cookies ()Print("Cookiesafter:")    Print(Cookiesafter)#The cookie is stored in the list, which is Dict    #The comparison found that after logging in the cookie was 4 more dict than before logging in.     #The following codes are 1, 4, 7, 8, respectively .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 ()#these four cookies will be obtained as parameters, passed to the function using a cookie login, as followsCookielogin (cookie1,cookie2,cookie3,cookie4)defCookielogin (cookie1,cookie2,cookie3,cookie4):Print("+++++++++++++++++++++++++")    Print("Cookielogin")    Print("cookie2:%s"%cookie2)Print("cookie4:%s"%cookie4) Driver=Webdriver. Chrome () Driver.maximize_window ()#Clear Cookiesdriver.delete_all_cookies () time.sleep (3) driver.get (URL)#Add a cookie after you open the browser and add the access addressDriver.add_cookie (cookie1) Driver.add_cookie (cookie2) Driver.add_cookie (cookie3) Driver.add_cookie (cookie 4)Print("Cookies")    #print a cookie and compare it to a cookie that is normally logged on above    Print(Driver.get_cookies ()) Time.sleep (5)    #To refresh the page, you can see the login that is already logged in, which is done using cookies. 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:pycharmImportYaml,time,os fromSeleniumImportWebdriverurl='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 the verification code:")#manually enter the verification codeSecurity_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 hibernation, so that the cookie is the login cookie, otherwise it may be printed or pre-logon cookieTime.sleep (5) Cookiesafter=driver.get_cookies () len1=Len (cookiesafter)#already know how many cookies are required, this requires a 3rd cookie, so select cookie subscript 2Cookie1 = cookiesafter[2]#gets the path of the current fileFilenamepath = Os.path.split (Os.path.realpath (__file__)) [0]#stitching Config.yaml File Absolute pathYamlpath = Os.path.join (Filenamepath,'Config.yaml')#write open file with overwriteFW = Open (Yamlpath,'W', encoding='Utf-8')#Building Datadata = {"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:pycharm fromSeleniumImportWebdriverImportTime,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 filef = open (Yamlpath,'R', encoding='Utf-8') Cont=f.read () Conf=yaml.load (cont)#Read Cookie valueCookie1 = Conf.get ("Cookie1")#Add a cookieDriver.add_cookie (cookie1)Print("Cookies")Print(Driver.get_cookies ()) Time.sleep (5)#here to regain the address, because some systems, not logged in status, 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 statusDriver.refresh () time.sleep (5) Driver.quit ()

The above should be considered as a detailed introduction of the use of cookies to login ideas. What problems can be added to the group communication, or attention to the public number, to communicate. We hope to help you.

Python Selenium Cookie Bypass Verification Code implementation login

Related Article

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.