问题
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")
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:
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'
回答1:
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")
来源:https://stackoverflow.com/questions/65813792/recaptcha-download-audio-file