Selenium(18): operation of bypassing verification code through cookie

Processing of verification code

For web applications, verification codes need to be entered in many places, such as login and posting, and there are many types; In the process of login / core operation, the system will generate random verification code pictures for verification before subsequent operations

The method to solve the verification code is as follows:

1. Develop a universal verification code (recommended)
2. Turn off the verification code function in the test environment (recommended) (development configuration)
3. Picture recognition technology (unstable)
4. Call development to generate verification code interface (cooperate with development)
5. Third party verification code platform (coding rabbit)
6. cookie bypass verification code (recommended)

cookie processing

  If we need to verify whether there are cookies in the browser, because real cookies cannot pass the white box and integration test, webdriver can read, add and delete cookie information

webdriver operates cookie s as follows:

get_cookies()   Get all cookie information
get_cookie(name)   Returns cookie information for a specific name
add_cookie(cookie_dict)   To add a cookie, you must have name and value values
delete_cookie(name)   Delete certain portions of cookie information
delete_all_cookies()   Delete all cookie information

 

Example: using cookie s to complete the password free application of the website

The first step of the code idea: just to write the cookie information after login into excel and prepare for password free login later

1. Open the login page of the website
2. Wait for 60s. At this time, manually enter the user name, password and verification code to log in
3. Get post login cookie
4. Write the cookie after login into excel

The code is as follows:

Preconditions: importing xlwt Libraries

import os
import time
import xlwt
from selenium import webdriver

current_path = os.path.dirname(os.path.abspath(__file__))  # current path
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver route
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie etc.

driver.get('http://47.107.187.54/zentao/www/index.php?m=user&f=login')  # Open Zen address

workbook = xlwt.Workbook(encoding='utf-8')  # set up workbook object
sheet = workbook.add_sheet('Sheet01')  # newly added sheet Page name
sheet.write(0,0,'number')  # Write values through row and column coordinates
sheet.write(0,1,'name')
sheet.write(0,2,'value')
sheet.write(0,3,'path')
sheet.write(0,4,'domain')

time.sleep(30)  # Manually enter user name, password, verification code and time
cookies = driver.get_cookies()  # Get after login cookie information

for i in range(1,len(cookies)+1):  # ergodic cookie And write the value through the row and column coordinates
    sheet.write(i,0,i)
    sheet.write(i, 1,cookies[i-1]['name'])
    sheet.write(i, 2,cookies[i-1]['value'])
    sheet.write(i, 3,cookies[i-1]['path'])
    sheet.write(i, 4,cookies[i-1]['domain'])

workbook.save('test.xls')  # Save and set excel Name of

 

Code idea of step 2:

1. Open the login page of the website
2. Use add_ The cookie () method adds the cookie information in excel to the cookie
3. Refresh the website to complete the automatic login operation

The code is as follows:

Preconditions: importing xlrd Libraries

import os
import time
import xlrd
from selenium import webdriver


current_path = os.path.dirname(os.path.abspath(__file__))  # current path
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver route
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie etc.

driver.get('http://47.107.187.54/zentao/www/index.php?m=user&f=login')  # Open Zen address
workbook = xlrd.open_workbook('test.xls')  # Open the specified excel file
sheet = workbook.sheet_by_name('Sheet01')  # The specified was found sheet page

# ergodic sheet Valid lines in the page excel in cookie Add information to cookie In, login free is realized
for i in range(1,sheet.nrows):
    driver.add_cookie( {'name':sheet.cell_value(i,1),'value':sheet.cell_value(i,2),
                       'path':sheet.cell_value(i,3),'domain':sheet.cell_value(i,4)} )


time.sleep(3)
driver.refresh()  # Refresh

 

Tags: Selenium

Posted on Wed, 01 Dec 2021 01:37:30 -0500 by dh526