selenium截取特定区域图片识别验证码

python 复制代码
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from PIL import Image
import ddddocr
import time
driver = webdriver.Chrome()
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
# 忽略证书错误
option.add_argument('--ignore-certificate-errors')
# 忽略 Bluetooth: bluetooth_adapter_winrt.cc:1075 Getting Default Adapter failed. 错误
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# 忽略 DevTools listening on ws://127.0.0.1... 提示
option.add_experimental_option('excludeSwitches', ['enable-logging'])
# 获取driver对象, 并将配置好的option传入进去 运行结束不关闭窗口
driver = webdriver.Chrome(options=option)
driver.get('https://xatu.168wangxiao.com/web/login?redirect=%2Fhome')

# 防止图片识别错误 try except 处理
while True:
#获取图片所在的div 或者可以直接获取图片得id 我这个图片比较麻烦 所以我获取得外层得div
    try:
        svgHeml=driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div[1]/div/form/div[4]/div/div[2]/div')

        # 获取元素的位置和尺寸
        location = svgHeml.location
        size = svgHeml.size
        
        # 计算四个角的坐标
        x1 = location['x']
        y1 = location['y']
        x2 = location['x'] + size['width']
        y2 = location['y'] + size['height']
        
        # 输出四个角的坐标
        print("左上角坐标: (x1, y1) = ({0}, {1})".format(x1, y1))
        print("右上角坐标: (x2, y1) = ({0}, {1})".format(x2, y1))
        print("左下角坐标: (x1, y2) = ({0}, {1})".format(x1, y2))
        print("右下角坐标: (x2, y2) = ({0}, {1})".format(x2, y2))


        # 截全图
        screenshot_path = 'screenshot.png'
        driver.save_screenshot(screenshot_path)
        
        # # time.sleep(3)
        
        # # # 截取指定区域
        im = Image.open(screenshot_path)
        region = im.crop((x1, y1, x2, y2))
        
        # # 保存截图
        cropped_path = 'result_screenshot.png'
        region.save(cropped_path)
        start = time.time() # 开始时间

        # 1. 创建DdddOcr对象
        ocr = ddddocr.DdddOcr(show_ad=False)

        # 2. 读取图片
        with open(cropped_path, 'rb') as f:
            img = f.read()

        # 3. 识别图片内验证码并返回字符串
        result = ocr.classification(img)
        print("识别结果:",result)
    except:
        # 以防万一 图片识别错误 重新点击图片 重新获取验证码 执行try
        driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div/div[2]/div[1]/div/form/div[4]/div/div[2]/div').click()
        time.sleep(3)

参考大佬链接
Selenium 自动化截取网页指定区域

相关推荐
武陵悭臾4 小时前
网络爬虫学习:借助DeepSeek完善爬虫软件,实现模拟鼠标右键点击,将链接另存为本地文件
python·selenium·网络爬虫·pyautogui·deepseek·鼠标右键模拟·保存链接为htm
2301_793069826 小时前
Java和SQL测试、性能监控中常用工具
java·sql·selenium
刽子手发艺20 小时前
Selenium+OpenCV处理滑块验证问题
opencv·selenium·webpack
bst@微胖子20 小时前
Python高级语法之selenium
开发语言·python·selenium
王小义笔记20 小时前
Postman如何流畅使用DeepSeek
开发语言·测试工具·lua·postman·deepseek
程序员杰哥1 天前
如何写出优秀的测试用例?
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
奔跑吧邓邓子2 天前
【Python爬虫(14)】解锁Selenium:Python爬虫的得力助手
开发语言·爬虫·python·selenium
程序员 小濠2 天前
2024Selenium自动化常见问题及解决方式!
自动化测试·软件测试·测试工具·单元测试·测试用例·接口测试·性能测试
测试19982 天前
功能测试与接口测试详解
自动化测试·软件测试·python·功能测试·测试工具·测试用例·接口测试
程序员三藏2 天前
Postman接口测试的cookie,token,session....鉴权
自动化测试·软件测试·测试工具·职场和发展·测试用例·接口测试·postman