2

I want to, click on the button to resolve the captcha through the audio, but selenium does not detect the specified "id".

browser.get("https://www.google.com/recaptcha/api2/demo")
mainWin = browser.current_window_handle  
iframe = browser.find_elements_by_tag_name("iframe")[0]
browser.switch_to_frame(iframe)
CheckBox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))).click()
sleep(4)
audio = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-audio-button"))).click()
2
  • captcha is made to stop bot activity, and selenium is kind of in that category you cant do much Commented Nov 27, 2019 at 20:13
  • And there is no way to prevent me from detecting my bot? Commented Nov 27, 2019 at 20:59

2 Answers 2

5

To click() on the button to resolve the captcha through the audio as the desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use the following Locator Strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    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:\WebDrivers\chromedriver.exe')
    driver.get("https://www.google.com/recaptcha/api2/demo")
    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-anchor"))).click()
    driver.switch_to.default_content()
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()
    
  • Browser Snapshot:

audio_captcha


Reference

Ways to deal with #document under iframe


Outro

You can find a couple of relevant discussions in:

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thanks for answering. It really didn't work, it records the following error: raise TimeoutException (message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
@Msthiue12 At which line are you seeing TimeoutException? Checkout the updated answer and let me know the status.
1

Very useful, just put your attention, that text: 'recaptcha challenge' in selector below depends from regional settings/language:

     WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.