如何使用Selenium和Python与reCAPTCHA音频元素进行交互 [英] How to interact with the reCAPTCHA audio element using Selenium and Python

查看:371
本文介绍了如何使用Selenium和Python与reCAPTCHA音频元素进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要单击按钮以通过音频解析验证码,但是硒无法检测到指定的"id".

I want to, click on the button to resolve the captcha through the audio, but selenium does not detect the specified "id".

browser.get("https://www.google.com/recaptcha/api2/demo")
mainWin = browser.current_window_handle  
iframe = browser.find_elements_by_tag_name("iframe")[0]
browser.switch_to_frame(iframe)
CheckBox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))).click()
sleep(4)
audio = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-audio-button"))).click()

推荐答案

在按钮上的click()上通过音频解析验证码,因为所需元素在<iframe>内,因此您必须:

To click() on the button to resolve the captcha through the audio as the desired elements are within an <iframe> so you have to:

  • 诱导 WebDriverWait 以使所需的框架可用并切换到.

诱导 WebDriverWait 以使所需的元素可点击.

您可以使用以下定位器策略:

代码块:

     from selenium import webdriver
     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC

     options = webdriver.ChromeOptions() 
     options.add_argument("start-maximized")
     options.add_experimental_option("excludeSwitches", ["enable-automation"])
     options.add_experimental_option('useAutomationExtension', False)
     driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
     driver.get("https://www.google.com/recaptcha/api2/demo")
     WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
     driver.switch_to.default_content()
     WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
     WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()

  • 浏览器快照:

  • Browser Snapshot:

    在iframe下处理#document的方式

    您可以在以下位置找到一些相关的讨论:

    You can find a couple of relevant discussions in:

    • How to click on the reCaptcha using Selenium and Java
    • CSS selector for reCaptcha checkbok using Selenium and vba excel
    • Find the reCAPTCHA element and click on it — Python + Selenium

    这篇关于如何使用Selenium和Python与reCAPTCHA音频元素进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆