1

Please let me know of what I'm doing wrong

Using the code from: How to interact with the reCAPTCHA audio element using Selenium and Python

I want next to get the src file for the audio to download it, so I wrote exactly after the end of the provided code the following:

# get the mp3 audio file
src = driver.find_element_by_id("audio-source").get_attribute("src")

But Python returns run time error saying:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="audio-source"]"}
  (Session info: chrome=88.0.4324.96)

Note: I copied that code as is, and only added the last line

For your convince here is my full code:

def tmp():
    from selenium.webdriver.common.keys import Keys

    # recaptcha libraries
    import speech_recognition as sr
    import urllib
    # import pydub

    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='/Users/ahmad/Desktop/chromedriver')
    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()

    # get the mp3 audio file
    src = driver.find_element_by_id("audio-source").get_attribute("src")

enter image description here


I tried on nearby element the following to get it's href value and it worked, my only problem is with what is above:

src=WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "a.rc-audiochallenge-tdownload-link"))).text

src=src.get_attribute('href')

print(src)

the element it worked on was:

enter image description here


I tried this too:

src=WebDriverWait(driver, 10).until(
    EC.invisibility_of_element((By.XPATH, "//*[@id=\"audio-source\"]")))

src=src.get_attribute('src')

But I get an error:

src=src.get_attribute('src')

AttributeError: 'bool' object has no attribute 'get_attribute'

  • The code as shown cannot be the full code. It at least has wrong indentation and misses imports. – MisterMiyagi Jan 20 '21 at 18:04
  • @MisterMiyagi fixed it but that didn't have any impact especially that it's the same code posted an answer there –  Jan 20 '21 at 18:15
  • The point is not to fix it for you. The point is to provide a [mcve] for us. – MisterMiyagi Jan 20 '21 at 18:25
  • an example is already provided, the google page is publicly available too and I have added an image of its html code, I'm already inside that frame but don't know what's the problem –  Jan 20 '21 at 18:31
  • I am not sure how you passed this line `iframe[src^='https://www.google.com/recaptcha/api2/anchor` because there is no such iframe at `https://www.google.com/recaptcha/api2/demo` – Alexey R. Jan 20 '21 at 18:38
  • It's in the code of the other question and runs find, the line I added causes the issue –  Jan 20 '21 at 19:30
  • @AlexeyR. I have added an image to that frame –  Jan 20 '21 at 19:33

1 Answers1

2
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()
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, ".rc-audiochallenge-play-button button")))

# get the mp3 audio file

src = driver.find_element_by_id("audio-source").get_attribute("src")
print(src)

Just add one more wiat for the .rc-audiochallenge-play-button button

to download you should use:

import urllib.request
urllib.request.urlretrieve(src, "src.mp3")
PDHide
  • 18,113
  • 2
  • 31
  • 46
  • why in wait you have click? –  Jan 20 '21 at 20:48
  • i removed it you don't need it even if its there its ok – PDHide Jan 20 '21 at 20:49
  • Plus, please note 'audio-source' is different and not contained in audiochallenge-play-button –  Jan 20 '21 at 20:49
  • i am not retrieving that you should wait for that audio play prompt to appear before you could retrieve src attribute , src attribute is still retreived from the same audo-source – PDHide Jan 20 '21 at 20:51