简体   繁体   中英

How to save playing audio with Selenium Python

I'm developing a captcha solver using IBM Watson and all is well, I just need to save the playing audio to a file which can be then resolved using watson. I don't know how to go about that and I didn't find anything here. If possible I don't want some complicated requests etc, just save the playing audio to a file. Or download the audio, but I tried using chrome_options to set download location, but it just didn't work Any help will be really appreciated

my code:

import os
import time
import random
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from multiprocessing import Process
import ibm_watson
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson.websocket import RecognizeCallback, AudioSource

chrome_options = Options()
chrome_options.add_argument("--mute-audio")
chrome_options.add_argument ("download.default_directory=/home/valentino/")
driver = webdriver.Chrome(options=chrome_options)

apikeywatson = 'C2f79A8ENbeUmWw-1DwTMd_v4IgCdCjqKpx21PsRaKan'
urlwatson = 'https://api.eu-de.speech-to-text.watson.cloud.ibm.com/instances/9a22253e-7fc5-4c67-b85b-5ad54db8282d'
authibm = IAMAuthenticator(apikeywatson)
stt = SpeechToTextV1(authenticator=authibm)
stt.set_service_url(urlwatson)

driver.get('https://client-demo.arkoselabs.com/github')
time.sleep(4)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='https://client-api.arkoselabs.com/fc/gc/']")))
time.sleep(2)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[class='fc_meta_audio_btn']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "audio_play"))).click()

I believe I have been through a similar situation. If your file is successfully downloading, but downloading in default directory, and not your desired directory, I will let you know what and how can you get around this issue.

  1. Dont use relative path, try using absolute path:

    chrome_options.add_argument (f"download.default_directory={}/home/valentino/")

  2. That will probably not work, try replacing forward slash with backward slash:

    chrome_options.add_argument (f"download.default_directory={}\\home\\valentino\\")

  3. If that worked for you, you are good to go. But it didn't work for me. I had to adopt an ugly turnaround for this problem by manually moving the file from downloaded folder to my desired folder. You can use something like this:

    from shutil import move

    #verify this path as it varies from OS to OS
    default_file_download_path = 'C:\\Users\\UserName\\Downloads\\' 
    destination_path = 'home\\valentino\\'
    
    downloaded_file_name = [x for x in os.listdir(default_file_download_path)
                            if "audio_verification_challenge" in x][0]
    
    move(default_file_download_path+downloaded_file_name , destination_path+downloaded_file_name)

Yes, the last option probably looks very ugly but it was the only way I could make it work for my use case.


UPDATE

If you closely inspect the HTML, they provide a sweet SRC link for every audio file. You need to retrieve the file from that SRC by using simple requests call and then save it in locally. I believe this is the easiest and fastest way.

driver.get('https://client-demo.arkoselabs.com/github')
time.sleep(4)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[src^='https://client-api.arkoselabs.com/fc/gc/']")))

audio_src = driver.find_element_by_xpath('//audio[@preload="auto"]').get_attribute('src')
content = requests.get(audio_src).content
# save the content into a file where you would want to
open('your_desired_location\\captcha_file.wav', 'wb').write(content)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM