Selenium automated testing - bypassing verification code through cookies

Verification code processing

  For web applications, verification codes are required in many places, such as logging in and posting, and there are various types; during the login/core operation process, the system will generate random verification code pictures, which must be verified before subsequent operations can be carried out.

Here’s how to solve the verification code:

1. Develop a universal verification code (recommended)
2. Turn off the verification code function in the test environment (recommended) (development configuration)
3. Image recognition technology (unstable)
4. Call the development and generation verification code interface (cooperate with development)
5. Third-party verification code platform (Code Rabbit)
6. Cookie bypasses verification code ( recommended )

cookie handling

If we need to verify whether cookies exist in the browser, because real cookies cannot be completed through white box and integration testing, webdriver can read, add and delete cookie information.

The method for webdriver to operate cookies is as follows:

get_cookies() Gets all cookie information
get_cookie(name) Returns cookie information for a specific name
add_cookie(cookie_dict) Adds a cookie, which must have name and value
delete_cookie(name) Delete a specific part of cookie information
delete_all_cookies() Delete all cookie information

Note: Dictionary parameters are placed in add_cookie(). The cookie dictionary generally needs to contain fields such as name, value, domain, path, etc. If a cookie has an Expirse field, it must be removed.

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

Practical example: Using cookies to complete the password-free application of the website

The code idea for the first step: just to write the cookie information after login into excel to prepare for the subsequent password-free login.

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

code show as below:

Precondition: import xlwt library

import os
import time
import xlwt
from selenium import webdriver

current_path = os.path.dirname(os.path.abspath(__file__))  # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路径
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等

driver.get('http://47.107.187.54/zentao/www/index.php?m=user&f=login')  # 打开禅道地址

workbook = xlwt.Workbook(encoding='utf-8')  # 设置workbook对象
sheet = workbook.add_sheet('Sheet01')  # 新增sheet页名称
sheet.write(0,0,'number')  # 通过行列坐标写入值
sheet.write(0,1,'name')
sheet.write(0,2,'value')
sheet.write(0,3,'path')
sheet.write(0,4,'domain')

time.sleep(30)  # 手动输入 用户名、密码、验证码时间
cookies = driver.get_cookies()  # 获取登录后的cookie信息

for i in range(1,len(cookies)+1):  # 遍历cookie的值,并通过行列坐标写入值
    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')  # 保存并设置excel的名称

Code idea for the second step:

1. Open the login page of the website
2. Add the cookie information in excel to the cookie using the add_cookie() method
3. Refresh the website to complete the automatic login operation

code show as below:

Precondition: import xlrd library

import os
import time
import xlrd
from selenium import webdriver


current_path = os.path.dirname(os.path.abspath(__file__))  # 当前路径
driver_path = os.path.join(current_path,'../webdriver/chromedriver.exe')  # driver路径
driver = webdriver.Chrome(executable_path=driver_path)  # Firefox,Ie等

driver.get('http://47.107.187.54/zentao/www/index.php?m=user&f=login')  # 打开禅道地址
workbook = xlrd.open_workbook('test.xls')  # 打开指定的excel文件
sheet = workbook.sheet_by_name('Sheet01')  # 找到指定的sheet页

# 遍历sheet页中有效的行,在把excel中cookie信息添加到cookie中,实现免登录
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()  # 刷新

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/132818439