I'm trying to save a file of a Captcha sound button to turn the sound into a string. I thought of saving it in .wav that is in the HTML in href and then using the speech recogniti module, because I am not getting it just with the sound of the website button when clicking. The website is https://servicos.receita.fazenda.gov.br/Servicos/CPF/ConsultaSituacao/ConsultaPublica.asp?Error=5
Basically I want to solve the captcha in "Realizar consulta com Captcha sonoro" automatically. For this, I used selenium to do the automation and I'm trying to get the sound to solve it, but I can't. So, I changed my mind, and decided to save the sound in a .wav file and thus use speech recognition to return the audio as a string.
Bellow is the code:
import pandas as pd
from datetime import datetime, date, timedelta
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from msedge.selenium_tools import Edge,EdgeOptions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import speech_recognition
import pyaudio
import pyttsx3
options = EdgeOptions()
options.add_argument("start-maximized")
#options.add_argument("unhandledPromptBehavior")
options.add_argument("ignore")
#options.use_chromium = True
#webdriver_service = Service('C:/Users/F00010764/msedgedriver.exe')
nav=Edge(executable_path = 'C:/Users/F00010764/msedgedriver.exe', options=options) #o webdrive e o parâmetro mostrando o caminho
wait = WebDriverWait(nav, 20)
website_b3 ="https://servicos.receita.fazenda.gov.br/Servicos/CPF/ConsultaSituacao/ConsultaPublica.asp?Error=5" #Link do site b3
nav.get(website_b3) #entrando no website, nesse em especifico no site da BaseException
time.sleep(5)
#wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#main")))
wait.until(EC.element_to_be_clickable((By.ID, "id_captchasonoro"))).click()
time.sleep(5)
audio_src =wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#idAntiRobo > div > div > div:nth-child(2) > a:nth-child(1)"))).get_attribute('href')
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)
sr = speech_recognition.Recognizer()
with speech_recognition.Microphone() as source2:
sr.adjust_for_ambient_noise(source2)
audio2 = sr.listen(source2)
try:
print ("O áudio convertido é :" + sr.recognize_google(audio2,language = 'pt-BR'))
This is the way to try to capture the sound, but I'm not succeeding. It ends up returning unknowvalueerror
So I wanted to save it but I don't know how to get the sound using selenium and then save it as .wav
Someone help?