消息:没有这样的元素:无法定位元素:{"method":"css selector","selector":".recaptcha-checkbox-border"}

Message: no such element: Unable to locate element: {"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) 但它不起作用。这是我试图绕过验证码的 link:https://whitepages.co.nz/ycaptcha?next=%2Fwhite-all%2Fhalswell%2Fchristchurch%2F

如果我在选择器 class 或其他方面有误,请告诉我。

<iframe src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LepxvIZAAAAAPGd49tlErQ-2da9Bh-3yN_gCjul&amp;co=aHR0cHM6Ly93aGl0ZXBhZ2VzLmNvLm56OjQ0Mw..&amp;hl=en&amp;v=2Mfykwl2mlvyQZQ3PEgoH710&amp;size=normal&amp;cb=r4uh76mv0o72" width="304" height="78" role="presentation" name="a-taqeeo56s3nk" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox"></iframe>

获取站点密钥后切换到 iframe。

wait = WebDriverWait(driver, 10)
driver.get("https://whitepages.co.nz/ycaptcha?next=%2Fwhite-all%2Fhalswell%2Fchristchurch%2F")
print(driver.find_element_by_class_name("g-recaptcha").get_attribute("data-sitekey"))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#contentMainSearchResults > form > div > div > div > iframe")))

导入

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

reCAPTCHA<iframe> 中,因此您必须:

  • 诱导 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
    
  • 浏览器快照:


参考

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

  • Switch to an iframe through Selenium and python
  • How to interact with the reCAPTCHA audio element using Selenium and Python