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()