from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import cv2
import numpy as np
def solve_geetest(driver):
# 获取验证码图片
bg_img = driver.find_element(By.CLASS_NAME, "geetest_bg_slice").screenshot_as_png
bg_img = np.array(bytearray(bg_img), dtype=np.uint8)
bg_img = cv2.imdecode(bg_img, cv2.IMREAD_COLOR)
# 获取滑块图片
slide_img = driver.find_element(By.CLASS_NAME, "geetest_slider_button").screenshot_as_png
slide_img = np.array(bytearray(slide_img), dtype=np.uint8)
slide_img = cv2.imdecode(slide_img, cv2.IMREAD_COLOR)
# 图像处理,寻找缺口位置
# 这里省略具体的图像处理代码,通常涉及到灰度化、二值化、边缘检测等步骤
# 假设我们找到了缺口位置
gap_position = find_gap_position(bg_img)
# 控制滑块移动到指定位置
slider = driver.find_element(By.CLASS_NAME, "geetest_slider_button")
action_chains = ActionChains(driver)
action_chains.click_and_hold(slider).perform()
action_chains.move_by_offset(gap_position, 0).release().perform()
# 主函数
if __name__ == "__main__":
options = Options()
options.headless = True # 可选,开启无头模式
driver = webdriver.Chrome(options=options)
driver.get("https://your-target-website.com/login")
# 填写用户名和密码
username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")
username.send_keys("your_username")
password.send_keys("your_password")
# 触发验证码
login_button = driver.find_element(By.ID, "login_button")
login_button.click()
# 解决验证码
solve_geetest(driver)
# 登录
login_button.click()
# 等待登录完成
# ...
driver.quit()
import cv2
import numpy as np
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
def find_gap_position(bg_img):
# 将背景图片转换为灰度图
gray = cv2.cvtColor(bg_img, cv2.COLOR_BGR2GRAY)
# 二值化处理,以便更好地识别边缘
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 边缘检测
edges = cv2.Canny(thresh, 100, 200)
# 寻找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 寻找最大的轮廓,这通常代表缺口
max_contour = max(contours, key=cv2.contourArea)
# 计算轮廓的边界框
x, y, w, h = cv2.boundingRect(max_contour)
# 缺口位置通常为边界框的左上角坐标x
gap_position = x
return gap_position
# 主函数
if __name__ == "__main__":
options = webdriver.ChromeOptions()
options.headless = True # 开启无头模式
driver = webdriver.Chrome(options=options)
driver.get("https://your-target-website.com/login")
# 获取背景图片
bg_img = driver.find_element(By.CLASS_NAME, "geetest_bg_slice").screenshot_as_png
bg_img = np.array(bytearray(bg_img), dtype=np.uint8)
bg_img = cv2.imdecode(bg_img, cv2.IMREAD_COLOR)
# 获取滑块图片
slide_img = driver.find_element(By.CLASS_NAME, "geetest_slider_button").screenshot_as_png
slide_img = np.array(bytearray(slide_img), dtype=np.uint8)
slide_img = cv2.imdecode(slide_img, cv2.IMREAD_COLOR)
# 图像处理,寻找缺口位置
gap_position = find_gap_position(bg_img)
# 控制滑块移动到指定位置
slider = driver.find_element(By.CLASS_NAME, "geetest_slider_button")
action_chains = ActionChains(driver)
action_chains.click_and_hold(slider).perform()
action_chains.move_by_offset(gap_position, 0).release().perform()
# 登录操作
# ...