首页 > 解决方案 > 消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":".recaptcha-checkbox-border"}

问题描述

我正在尝试使用 Selenium 绕过验证码验证,但我不断收到此错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".recaptcha-checkbox-border"}

我已经尝试过使用sleep(20),但它不起作用。这是我试图绕过验证码的链接:https ://whitepages.co.nz/ycaptcha?next=%2Fwhite-all%2Fhalswell%2Fchristchurch%2F

如果我在选择器类中犯了错误,请告诉我。

标签: python-3.xseleniumiframerecaptchawebdriverwait

解决方案


reCAPTCHA在 an 内,因此<iframe>您必须:

  • 诱导WebDriverWait使所需的帧可用并切换到它

  • 诱导WebDriverWait使所需元素成为可点击的。

  • 您可以使用以下任一定位器策略

    • 使用CSS_SELECTOR

      driver.get("https://whitepages.co.nz/ycaptcha?next=%2Fwhite-all%2Fhalswell%2Fchristchurch%2F")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-border"))).click()
      
    • 使用XPATH

      driver.get("https://whitepages.co.nz/ycaptcha?next=%2Fwhite-all%2Fhalswell%2Fchristchurch%2F")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://www.google.com/recaptcha/api2/anchor')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='recaptcha-checkbox-border']"))).click()
      
  • 注意:您必须添加以下导入:

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

白页


参考

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


推荐阅读