篇幅较长,请大家耐心看完,建议收藏慢慢看,另外文末给大家准备了面试资料(面试题加学习资料等),需要自取!
做自动化 UI 测试时,遇到登录有验证码校验无法绕过时,可以使用下面的方法。
就是登录模块的这种腾讯云滑块验证码。
发现有缺口的图片和没有缺口的图片,url 里的 img_index 参数不同。(一个 img_index=1,一个 img_index=0)
所以用 PIL 库,对比 验证码图片 有缺口和无缺口的不同,计算出偏移量,模拟滑块滑动。
import random
import io
from PIL import Image
from urllib.request import urlopen
from selenium.webdriver.common.action_chains import ActionChains
import time
# 打开图片链接
def open_img_url(src):
img_url = urlopen(src, timeout=2).read()
img = Image.open(io.BytesIO(img_url))
# 恢复原始大小
img_resize = (int(i // 2) for i in img.size)
return img.resize(img_resize, Image.ANTIALIAS)
def get