I get the error
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject1\main.py", line 58, in <module>
browser.close_browser()
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Browser' object has no attribute 'close_browser'
and my code is
from pychrome import Browser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
Set the threshold decibel level
threshold_db = 30
Function to produce a loud beep
def beep():
print("Alert! Sound level reached.")
# Insert your code here to produce a loud beep
Function to check if sound level exceeds threshold and produce beep
def check_sound(message, data):
# Extract audio data from the message
audio_data = data['params'].get('audioBuffer')
if audio_data:
# Calculate the average intensity of the audio data
intensity = sum(abs(sample) for sample in audio_data) / len(audio_data)
volume_norm = intensity * 10
print(f"Current sound level: {volume_norm} dB")
if volume_norm > threshold_db:
beep()
Set up ChromeOptions to run headless
chrome_options = Options()
chrome_options.add_argument('--headless')
Start the Chrome browser
browser = Browser(url="http://127.0.0.1:9222")
try:
# Set up the ChromeDriver
driver = webdriver.Chrome(options=chrome_options)
# Open a webpage with audio content (replace with your desired URL)
driver.get('url') # Replace with your desired URL
# Enable the Network domain
browser.network.enable()
# Listen for audio events
browser.network.on('AudioContext.addAudioBuffer', check_sound)
# Let it run indefinitely (you may modify or add conditions to exit the loop)
while True:
time.sleep(1)
except Exception as e:
print(f"An error occurred: {str(e)}")
finally:
# Close the browser
if driver:
driver.quit()
# Close the Chrome browser
browser.close_browser()