【问题标题】:Message: no such element: Unable to locate element: {"method":"css selector","selector":".recaptcha-checkbox-border"}消息:没有这样的元素:无法找到元素:{"method":"css selector","selector":".recaptcha-checkbox-border"}
【发布时间】:2021-05-19 10:55:41
【问题描述】:

我正在尝试使用 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

如果我在选择器类或其他方面犯了错误,请告诉我。

【问题讨论】:

  • 您的对象位于“iframe”中 - 您需要切换到该框架才能使用 selenium 识别它...但是 - 如果此站点有验证码,它不希望您将其自动化,这将是您将面临的问题的冰山一角。验证码旨在阻止机器人和自动化 - 如果它们不起作用,人们就不会使用它们

标签: python-3.x selenium iframe recaptcha webdriverwait


【解决方案1】:

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

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

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

  • 您可以使用以下任一Locator Strategies

    • 使用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
    
  • 浏览器快照:


参考

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

【讨论】:

  • 非常感谢您的帮助,代码现在可以正常运行了。
【解决方案2】:
<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>

获得sitekey后切换到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

【讨论】:

  • 切换到 iframe 是否对 data-sitekey 有任何依赖?
  • 不是真的只是在他需要他的密钥进行验证码之后。他会把它放在下面。
猜你喜欢
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2020-09-30
  • 2023-03-12
相关资源
最近更新 更多