# -*- 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()
selenium处理点选验证码---图灵
最新推荐文章于 2024-05-14 11:03:01 发布