如何使用 Selenium 和 Python 与 reCAPTCHA 音频元素交互

Mst*_*e12 2 python iframe selenium recaptcha webdriverwait

我想,点击按钮通过音频解析验证码,但是selenium没有检测到指定的“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()
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 5

当click()所需的元素位于某个元素内时,单击该按钮可通过音频解析验证码<iframe>因此您必须:

\n
    \n
  • 诱导WebDriver等待所需的框架可用并切换到它。

    \n
  • \n
  • 诱导WebDriverWait等待所需元素可点击。

    \n
  • \n
  • 您可以使用以下定位策略:

    \n
  • \n
  • 代码块:

    \n
    from selenium import webdriver\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support import expected_conditions as EC\n\noptions = webdriver.ChromeOptions() \noptions.add_argument("start-maximized")\noptions.add_experimental_option("excludeSwitches", ["enable-automation"])\noptions.add_experimental_option(\'useAutomationExtension\', False)\ndriver = webdriver.Chrome(options=options, executable_path=r\'C:\\WebDrivers\\chromedriver.exe\')\ndriver.get("https://www.google.com/recaptcha/api2/demo")\nWebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^=\'https://www.google.com/recaptcha/api2/anchor\']")))\nWebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()\ndriver.switch_to.default_content()\nWebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title=\'recaptcha challenge\']")))\nWebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()\n
    Run Code Online (Sandbox Code Playgroud)\n
  • \n
  • 浏览器快照:

    \n
  • \n
\n

音频验证码

\n
\n

参考

\n

iframe下处理#document的方法

\n
\n

尾奏

\n

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

\n\n