Dealing with reCAPTCHA in Python Selenium

后端 未结 3 1165
离开以前
离开以前 2021-01-23 08:48

I need to automate a web page using python selenium, but it encounters a reCaptcha, which is in another frame. I want to solve the captcha, and continue the script by clicking t

相关标签:
3条回答
  • 2021-01-23 09:04

    You can simply wait for the checkbox to show the done icon and then the wait will be over

    Like in the code below

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Firefox()
    driver.get("http://somedomain/url_that_delays_loading")
    try:
        element = WebDriverWait(driver, 100).until(
            EC.presence_of_element_located((By.ID, "myDynamicElement"))
        )
    finally:
        driver.quit() here
    
    0 讨论(0)
  • 2021-01-23 09:08

    Once you fill in the Email and Password field to click on the recaptcha you can use the following Locator Strategies:

    • Code Block:

      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_experimental_option("excludeSwitches", ["enable-automation"])
      options.add_experimental_option('useAutomationExtension', False)
      driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://developer.syntecx.org/ptcl_ebills/signin.php")
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#email"))).send_keys("Asad_Ullah@stackoverflow.com")
      driver.find_element_by_css_selector("input#password").send_keys("Asad_Ullah")
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor?']")))
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.recaptcha-checkbox.goog-inline-block.recaptcha-checkbox-unchecked.rc-anchor-checkbox"))).click()
      driver.switch_to_default_content()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary.block.full-width.m-b"))).click()
      
    • Browser Snapshot:

    0 讨论(0)
  • 2021-01-23 09:12

    Don't switch to the iframe.

    Everything you need is in #g-recaptcha-response and [data-sitekey] which are both in main context.

    0 讨论(0)
提交回复
热议问题