selenium verification code processing of cookie Login

In the actual test you will often see the Log operation requires CAPTCHA Login

Common verification are the following:

  1. Verification code
  2. Picture identification  

           

  3. Picture identification verification slider

                  

 

      4. Simple calculation codes

There are several top for login authentication solutions:

1. allow developers to remove codes (codes related commented code)

2. Set universal codes (only for the first codes)

3. verification code recognition technology (mainly for picture identification)

       the python pytesseract library codes for identifying

4. Use cookie records login, automation scripts bypass the validation link

The following describes the use cookie log on to blog Park as columns, other solutions have the time to be supplemented later:

Garden blog login authentication for the third figure: Photo identification verification slider

Park blog login page:

 

The first step: Get the cookie

First, write a logon script as follows:

 Remember to choose to remember the password to log ##

 1 from selenium import webdriver
 2 from time import sleep
 3 
 4 Dirver = webdriver.Chrome()
 5 Dirver.maximize_window()
 6 Dirver.get("https://account.cnblogs.com/signin")
 7 Dirver.implicitly_wait(10)
 8 Dirver.find_element_by_id('LoginName').send_keys('username')
 9 Dirver.find_element_by_id('Password').send_keys('password')
10 sleep(60)
11 
12 #代码运行到此处时,手动点击登录按钮并进行图片滑块验证,完成后,以下代码会获取登录成功后的页面cookie
13 
14 cookies = Dirver.get_cookies()  
15 print(cookies)
16 Dirver.quit()


第二步:根据获取的cookie进行登录

 

 1 from selenium import webdriver
 2 from time import sleep
 3 
 4 
 5 Dirver = webdriver.Chrome()
 6 Dirver.maximize_window()
 7 Dirver.get("https://home.cnblogs.com/")
 8 Dirver.implicitly_wait(10)
 9 Dirver.add_cookie({'name':' ','value':''})  #输入第一步得到的值
10 Dirver.add_cookie({'name':'','value':''})
11 sleep(10)
12 Dirver.get("https://home.cnblogs.com/")
13 username = Dirver.find_element_by_css_selector('#header_user_right a:nth-child(2) ').text  #获取用户名
14 print(username)
15 welcome = Dirver.find_element_by_id('header_user_left').text
16 print(welcome)  #获取欢迎字样
17 sleep(3)
18 
19 Dirver.quit()

登陆成功后的页面如下:

 

关于自动化测试中的验证码登录的其他解决办法后续将一一补充

 

Guess you like

Origin www.cnblogs.com/1211-1010/p/11090727.html