【问题标题】:Click on spam with Python Selenium使用 Python Selenium 点击垃圾邮件
【发布时间】:2020-05-30 18:57:58
【问题描述】:

我想点击 Google ReCAPTCHA 测试中的复选框。为此(我认为)我必须点击以下跨度:

<span class="recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox" role="checkbox" aria-checked="false" id="recaptcha-anchor" tabindex="0" dir="ltr" aria-labelledby="recaptcha-anchor-label"><div class="recaptcha-checkbox-border" role="presentation"></div><div class="recaptcha-checkbox-borderAnimation" role="presentation"></div><div class="recaptcha-checkbox-spinner" role="presentation"><div class="recaptcha-checkbox-spinner-overlay"></div></div><div class="recaptcha-checkbox-checkmark" role="presentation"></div></span>

https://patrickhlauke.github.io/recaptcha/ 网站在这里(它只是一个带有 ReCAPTCHA 的空网站)。

我正在使用最新版本的 Chrome 驱动程序,它被定义为驱动程序。我试过了:

driver.find_element_by_tag_name("span").click()

driver.find_element_by_xpath('//*[@id="recaptcha-anchor"]').click()

driver.find_element_by_id('recaptcha-anchor').click()

driver.find_element_by_class_name('recaptcha-checkbox goog-inline-block recaptcha-checkbox-unchecked rc-anchor-checkbox').click()

但我总是收到以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {*this is every try different*}
  (Session info: chrome=83.0.4103.61)

我是 Python 的新手,这是我第一次尝试 Selenium。

【问题讨论】:

    标签: python html selenium checkbox webdriver


    【解决方案1】:

    复选框位于 iframe 中,因此您需要先使用 selenium 切换到该复选框。下面应该可以帮助您单击复选框,并且还可以让您访问 9 个框。

    首先确保您有以下导入

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

    下一步:

    # Switch to iframe to click the checkbox
    WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".g-recaptcha>div>div>iframe")))
    
    #click the checkbox
    driver.find_element_by_id('recaptcha-anchor').click()
    
    #switch back to the main content in order to switch to the next iframe we need
    driver.switch_to.default_content()
    
    #switch to the iframe that contains the 9 boxes
    WebDriverWait(driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='recaptcha challenge']")))
    
    #get the 9 boxes
    boxes = driver.find_elements_by_css_selector('#rc-imageselect-target button')
    

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 2013-05-01
      • 2014-04-14
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多