识别谷歌验证码

1.扩展插件识别 reCAPTCHA
captcha1.png

#-*- coding: utf-8 -*-

from selenium import webdriver
import time
import pyautogui
executable_path = r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(executable_path =executable_path)

def auto_click_captcha(count):  # 插件识别验证码
    print('count:', count)
    driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@title="reCAPTCHA"]'))
    driver.find_element_by_xpath('//span[@id="recaptcha-anchor"]').click()  # 点击验证码
    driver.switch_to.parent_frame()
    # if count>0:
        # time.sleep(2)
        # driver.switch_to.frame(self.driver.find_element_by_xpath('//iframe[contains(@title,"recaptcha ")]'))
        # driver.find_element_by_xpath('//button[@title="Get a new challenge"]').click() #刷新验证码
        # driver.switch_to.parent_frame()
    for i in range(3):
        time.sleep(2)
        left_click1 = pyautogui.locateOnScreen('captcha1.png')
        if left_click1: break
    print('left_click1::', left_click1)  # 返回屏幕所在位置
    if left_click1:
        url_x, url_y = pyautogui.center(left_click1)
        pyautogui.leftClick(url_x, url_y)
        return True
    return False


def check_success_captcha():
    count = 0
    while count < 2:
        try:
            auto_click_captcha(count)
        except Exception as e:
            pass
        flug = False
        recapt = driver.find_element_by_xpath('//iframe[@title="reCAPTCHA"]')
        location = recapt.location
        print('location:', location)
        driver.switch_to.frame(recapt)
        for i in range(3):
            time.sleep(2)
            aria_checked = driver.find_element_by_xpath('//span[@id="recaptcha-anchor"]')
            aria_checked = aria_checked.get_attribute('aria-checked')
            print('aria_checked:', aria_checked)
            if aria_checked == 'true':
                print('验证成功')
                flug = True
                break
        driver.switch_to.parent_frame()
        if flug: break
        print('try click')
        # driver.refresh()
        pyautogui.leftClick(location['x'], location['y'])
        count += 1

def verify_google_captcha():
    src = driver.execute_script(
        '''var src_data=document.querySelector('iframe[title="reCAPTCHA"]');if(src_data){return src_data.getAttribute('src')}''')
    # src = self.driver.find_element_by_xpath('//iframe[@title="reCAPTCHA"]').get_attribute('src')
    if src or 'https://www.google.com/recaptcha/api2/anchor' in driver.page_source:
        print('出现谷歌验证码')
        check_success_captcha()
    else:
        return

def run():
    url = 'https://www.google.com/recaptcha/api2/demo'
    driver.get(url)
    time.sleep(5)
    verify_google_captcha()
    input()

run()







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值