img_element = driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[1]")
hk_element = driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/div/canvas[2]")
yt_element = driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div[6]/div/div[1]/div[1]/div/a/div[1]/canvas")
active = False
except:
continue
# 执行js 获取改元素的data:url
img_data_b64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(22);",img_element)
hk_data_b64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(22);",hk_element)
yt_data_b64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(22);",yt_element)
#解码
img_data = base64.b64decode(img_data_b64)
hk_data = base64.b64decode(hk_data_b64)
yt_data = base64.b64decode(yt_data_b64)
#以二进制保存
with open("./imgs/bg.png","wb") as f:
f.write(img_data)
#print("[*]滑块验证码背景图已保存")
with open("./imgs/hk.png","wb") as f:
f.write(hk_data)
#print("[*]滑块验证码滑块图片已保存")
with open("./imgs/yt.png","wb") as f:
f.write(yt_data)
#print("[*]滑块验证码原图已保存")
print("[*]保存滑动验证码成功")
提取出来如下图所示,(hk\_jg.png是下一个模块处理后的结果,不用管)
计算距离的主要需要图片就是通过hk.png处理后的hk\_jg.png和bg.png两个图片
![](https://img-blog.csdnimg.cn/direct/49801820ead04c9e8116ef6ac7bc9b81.png)
接下来处理hk.png为hk\_jg.png
直接传入hk.png这种图片的路径,可以实现将带有白框的图片截取成只有图片的最小方框,例如: