selenium处理点选验证码---图灵

# -*- coding:utf-8 -*-
import time, os, base64, json, requests
# pip install pillow
from PIL import Image
from selenium import webdriver
# 读取ini文件当中的数据
from configparser import ConfigParser
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

cfg = ConfigParser()
cfg.read("data.ini")
username = cfg.get("username", "username")
password = cfg.get("password", "password")


class Login_Text(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver, 10)
    def chrome_settings(self):
        url = "https://www.geetest.com/demo/click-popup.html"
        self.driver.get(url)
        # 设置浏览器的长宽
        self.driver.set_window_size(1100, 950)

        # 点击验证--验证码图片出现
        self.wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="captcha"]/div[3]/div[2]/div[1]/div[3]/span[1]'))).click()

    def save_image(self):
        image_name = str(int(time.time() * 1000)) + ".png"
        if not os.path.exists("Image_Small"):
            os.makedirs("Image_Small")

        if not os.path.exists("Image_Big"):
            os.makedirs("Image_Big")
        # 等验证码的图片加载出来,之后在截图
        time.sleep(1)
        self.driver.save_screenshot("yzm.png")
        img = Image.open("yzm.png")

        # 根据坐标值,提取出验证码中的大小图 左上x,左上y,右下x,右下y
        image_small_offset = img.crop((526, 187, 704, 236))
        image_small_offset.save("./Image_Small/" + image_name)
        image_big_offset = img.crop((379, 242, 708, 573))
        image_big_offset.save("./Image_Big/" + image_name)
        def b64_api(username, password, img_path, ID):
            with open(img_path, 'rb') as f:
                b64_data = base64.b64encode(f.read())
            b64 = b64_data.decode()
            data = {"username": username, "password": password, "ID": ID, "b64": b64, "version": "3.1.1"}
            data_json = json.dumps(data)
            result = json.loads(requests.post("http://www.fdyscloud.com.cn/tuling/predict", data=data_json).text)
            return result
        image_small_path = "./Image_Small/" + image_name
        result_small_image = b64_api(username, password, image_small_path, ID="47834704")
        print(result_small_image)
        image_big_path = "./Image_Big/" + image_name
        result_big_image = b64_api(username, password, image_big_path, ID="47834704")
        print(result_big_image)

        # 将鼠标放在大图的左上角
        element = self.driver.find_element(By.CLASS_NAME,"geetest_mark")
        result_small = result_small_image["data"]["result"]
        print("result_small:", result_small)

        for i in range(0, len(result_small)):
            result = result_big_image["data"][result_small[i]]
            print("result:", result_small[i], result)
            ActionChains(self.driver).move_to_element(element).move_by_offset(-70 + int(result["X坐标值"]), 15 + int(result["Y坐标值"])).click().perform()
            time.sleep(3)


        self.wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div/div[2]/div[1]/div/div/div[3]/a/div"))).click()
        time.sleep(3)
        self.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="btn"]'))).click()


if __name__ == '__main__':
    login = Login_Text()
    login.chrome_settings()
    login.save_image()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值