如何使用 Selenium 和 Python 定位并单击 recaptcha 复选框

How to locate and click on the recaptcha checkbox using Selenium and Python

我已经尝试过多种方法来使用 selenium 单击此复选框,但我做不到。 Link: http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira 我已经尝试过使用 XPATH,CLASS_NAME 和其他人,但是 return 总是一样的:

no such element: Unable to locate element:

谁能帮帮我?

代码试验:

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By

from selenium.webdriver.chrome.options import Options
from fake_useragent import UserAgent
from capmonster_python import RecaptchaV2Task

class ChromeAuto:
    def __init__(self):
        options = Options()
        ua = UserAgent()
        self.userAgent = ua.random
        print(self.userAgent)
        options.add_argument(f'user-agent={self.userAgent}')
        self.driver_path = r'chromedriver'
        self.options = webdriver.ChromeOptions()
        self.options.add_argument('--profile-directory=1')
        self.options.add_experimental_option("excludeSwitches", ["enable-automation"])
        self.options.add_experimental_option("useAutomationExtension", False)
        self.captcha = RecaptchaV2Task("c6eea325a7a78273c062d2bb23a2a43d")

        self.chrome = webdriver.Chrome(
            self.driver_path,
            options=self.options
        )
    def test(self):
        self.chrome.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
        sleep(5)
        self.chrome.find_element(By.XPATH, '//*[@id="recaptcha-anchor"]/div[1]').click()

if __name__ == '__main__':
    chrome = ChromeAuto()
    chrome.test()

ReCaptcha checkbox is within an 所以你必须:

  • 诱导 WebDriverWait 所需的 帧可用并切换到它
  • 诱导 所需的 元素可点击
  • 您可以使用以下任一项
    • 使用CSS_SELECTOR:

      driver.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='reCAPTCHA']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
      
    • 使用 XPATH:

      driver.get('http://buscatextual.cnpq.br/buscatextual/email.do?metodo=apresentar&seqIdPessoa=246740&nomeDestinatario=Maria_Jos%E9_Panichi_Vieira')
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='reCAPTCHA']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
      

PS: Clicking on the checkbox opens the image selection panel.

  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


参考

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

  • Switch to an iframe through Selenium and python