Python 实现指定位置验证码自动破解与登录流程
本文介绍使用 Python 和 Selenium 自动化操作浏览器,通过超级鹰平台识别图形验证码坐标,并自动完成点击验证及登录的全过程。内容涵盖驱动创建、页面交互、坐标转换及鼠标模拟点击等关键步骤,适用于解决网页自动登录中的验证码难题。
Python 自动破解指定位置验证码思路
整体思路如下:
浏览器驱动与页面加载流程
首先创建浏览器驱动对象以控制浏览器环境。
接着加载目标网站的登录页面。
等待页面元素完全加载完毕,确保交互稳定性。
登录信息输入与验证码处理
将页面切换至用户名和密码登录模式。
输入手机号,此步骤需等待输入框元素出现后获取并进行输入操作。
随后输入对应的登录密码。
点击触发验证的按钮,调出验证码图片。
获取弹出的验证码图片元素供后续处理。
利用超级鹰打码平台对验证码图片进行识别,获取图形点击的坐标信息。
坐标转换与鼠标点击模拟
获取到坐标数据后,将 x 和 y 坐标值分别除以 2;这是因为当前电脑分辨率较高(通常是标准分辨率的两倍),若为普通分辨率则无需除以 2 可直接使用。
控制鼠标移动到计算后的坐标点位置,并执行点击动作。
最终登录操作
最后点击登录按钮,完成整个登录流程。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
# 导入超级鹰
from chaojiying import chaojiying
#根据系统,可能截图不成功,需要使用无头浏览,mac系统可以不设置
options=webdriver.ChromeOptions()
options.headless=True
driver=webdriver.Chrome(options=potions)
driver.get('http://www.zhaopingou.com/signin')
driver.find_element_by_class_name('li02').click()
wait=WebDriverWait(driver,20,0.5)
# 账号登录
login_phone=wait.until(EC.visibility_of_element_located((By.ID,'pwd_login_phone')))
login_phone.send_keys('17724035140')
# 密码
driver.find_element_by_id('form_login_password').send_keys('961831740hzll')
# 点击获取图片
captcha = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="phone_login_pwd"]//iframe[starts-with(@id, "captcha_widget")]')))
captcha.click()
# 点击
# 保存图片(可以不保存)
captcha_element = wait.until(EC.visibility_of_element_located((By.XPATH, '//body[@class="graybc"]//iframe[starts-with(@id, "captcha_frame")]')))
captcha_element.screenshot('zhaopingou.png')
# 将图片转换为二进制
bytes_img=captcha_element.screenshot_as_png
# print(bytes_img)
result=chaojiying.post_pic(bytes_img,'9101')
x,y=result['pic_str'].split(',')
print(x,y)
x=int(x)
y=int(y)
# ActionChains(driver).move_to_element_with_offset(bytes_img,x,y).click().perform()
ActionChains(driver).move_to_element_with_offset(captcha_element, x, y).click().perform()
time.sleep(2)
driver.find_element_by_id('free_login_btn').click()
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[0])
# time.sleep(5)
driver.quit()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













