废话不多说直接开干!
from selenium import webdriver
# 导入配置
from selenium.webdriver.chrome.options import Options
import time
from PIL import Image
# 导入动作链
from selenium.webdriver.common.action_chains import ActionChains
import random, string
# 定义计算移动距离的函数
def get_difference(image1,image2):"""循环每一个点,计算出对应的像素值"""# 外层循环循环长度for i in range(image1.width):# 内层循环循环宽度for j in range(image1.height):# 找出缺口if not is_similar(image1,image2,i,j):return i# 定义找出缺口位置的函数
def is_similar(image1,image2,x,y):# 计算RGB值pixel1 = image1.getpixel((x,y))pixel2 = image2.getpixel((x,y))# 设置一个容差范围,设置30位容差范围if abs(pixel1[0] - pixel2[0]) >30 and abs(pixel1[1] - pixel2[1]) > 30 and abs(pixel1[2] - pixel2[2])>30:return Falsereturn True# 定义获取运动轨迹函数
def get_tracks(distance):"""v = v0+atx = v0t+1/2at**2"""# 定义存放运动轨迹的列表tracks = []# 定义初速度v = 0# 定义单位时间t = 0.5# 定义匀加速运动和匀减速运动的分界线mid = distance * 4/5# 定义当前位移current = 0# 为了一直移动,定义循环while current < distance:if mid > current:a = 2else:a = -3v0 = v# 计算位移x = v0 * t + 1/2*a*t**2# 计算滑块当前位移current += x# 计算末速度v = v0+a*ttracks.append(round(x))return tracks# 实例化Options对象
options = Options()# 启动开发者模式
options.add_experimental_option('excludeSwitches',['enable-automation'])
options.add_experimental_option('useAutomationExtension',False)# 2. 调用浏览器
driver = webdriver.Chrome(executable_path=r'D:\chorme\Chrome\Application\chromedriver.exe',options=options)# 将webdriver属性干掉
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{"source":'Object.defineProperty(navigator,"webdriver",{get:()=>undefined})'}
)# 最大化窗口
driver.maximize_window()# 3. 请求
driver.get(url='https://www.sf-express.com/we/ow/chn/sc/waybill/waybill-detail/SF1638641302904')# 出现滑块验证码
# 发现:每一个滑块移动的距离都是不同的,差距也比较大,所以,不能够写一个固定的移动距离
# 思路:将移动距离计算出来
# 核心:
# 1. 找出无缺口的图片
# 2. 使用有缺口的图片和无缺口的图片做对比
# 目的:获取移动的距离(确定缺口位置)# 休眠,等待验证码加载
time.sleep(3)# 截图,获取有缺口图片
driver.switch_to.frame('tcaptcha_iframe')
# elements = driver.find_element_by_xpath('//*[@id="tcWrap"]')
driver.save_screenshot('quekou.png')# 找到图片
# driver.switch_to.frame('tcaptcha_iframe')
element = driver.find_element_by_xpath('//*[@id="tcWrap"]')# 获取图片位置
# loaction:获取元素位置
# size:获取元素大小
print(element.location)
print(element.size)# 计算截图范围
left = element.location['x']
right = element.location['x'] + element.size['width']
top = element.location['y']
bottom = element.location['y'] + element.size['height']# 打开图片
im = Image.open('quekou.png')
# # 开始局部截图
im = im.crop((left+502,top+150,right+502,bottom+150))
im.save('quekou_jubu.png')# 执行js,显示无缺口图片
# driver.execute_script('document.getElementsByClassName("geetest_canvas_fullbg")[0].style="display:block"')
driver.find_element_by_xpath('//*[@id="slideBlock"]')
#
# # 截取无缺口图片
driver.save_screenshot('wuque.png')
#
im = Image.open('wuque.png')
# 开始局部截图
im = im.crop((left+560,top+710,right,bottom))
# im.save('wuque_jubu.png')# 将两张图片进行对比,计算移动距离
# wuque_jubu = Image.open('wuque_jubu.png')
# quekou_jubu = Image.open('quekou_jubu.png')
# distance = get_difference(wuque_jubu,quekou_jubu)
# 142 150 155ua_list = [ 142,150, 155,160]for ii in ua_list:distance = ii# 获取滑块huakuai = driver.find_element_by_xpath('//*[@id="tcaptcha_drag_thumb"]')ActionChains(driver).click_and_hold(on_element=huakuai).perform()ActionChains(driver).move_by_offset(xoffset=(distance+54)*0.8,yoffset=0).perform()tracks = get_tracks((distance+54)*0.2)for track in tracks:ActionChains(driver).move_by_offset(xoffset=track,yoffset=0).perform()time.sleep(1)# 释放鼠标ActionChains(driver).release().perform()time.sleep(5)try:status = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[2]/div[3]/div[2]/div/div[2]/div/ul[1]/li[1]/span').texttime = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[2]/div[3]/div[2]/div/div[2]/div/ul[1]/li[3]/span').textcontent = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[2]/div[3]/div[2]/div/div[2]/div/ul[1]/li[4]/span').textprint(status,time,content)driver.quit()breakexcept Exception:passprint('succ')