from PIL import Image from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait as Wait from selenium.webdriver.support import expected_conditions as Expect import new.anti.easing as easing import time import cv2
defget_pictures(browser): browser.execute_script(''' var x = document.getElementsByClassName("geetest_canvas_slice geetest_absolute"); x[0].style.display="none"; var y = document.getElementsByClassName("geetest_canvas_fullbg geetest_fade geetest_absolute"); y[0].style.display="none"; ''') time.sleep(0.8) # 带缺口的验证码图像的全屏截图 browser.save_screenshot('img1.jpg') # 裁剪验证码图像 crop_picture('img1.jpg')
browser.execute_script(''' var x = document.getElementsByClassName("geetest_canvas_bg geetest_absolute"); x[0].style.display="none"; var y = document.getElementsByClassName("geetest_canvas_fullbg geetest_fade geetest_absolute"); y[0].style.display="block"; ''') time.sleep(0.8) # 不带缺口的验证码图像的全屏截图 browser.save_screenshot('img2.jpg') # 裁剪验证码图像 crop_picture('img2.jpg')
# 使元素恢复成可见的 browser.execute_script(''' var x = document.getElementsByClassName("geetest_canvas_bg geetest_absolute"); x[0].style.display="block"; var y = document.getElementsByClassName("geetest_canvas_slice geetest_absolute"); y[0].style.display="block"; ''')
其中,裁剪函数如下:
1 2 3 4 5 6 7 8 9
defcrop_picture(filename): left = 830 top = 335 right = left + 260 bottom = top + 160
im = Image.open(filename) im = im.crop((left, top, right, bottom)) im.save(filename)
defmatch(i1, i2, t): p1 = [int(p) for p in i1] p2 = [int(p) for p in i2] if abs(p1[0] - p2[0]) < t and abs(p1[1] - p2[1]) < t and abs(p1[2] - p2[2]) < t: returnTrue returnFalse