How to mute all sounds in chrome webdriver with selenium
<p>I want to write a script in which I use selenium package like this:</p>

<pre><code>from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=hdw1uKiTI5c")
</code></pre>

<p>now after getting the desired URL I want to mute the chrome sounds.
how could I do this?
something like this:</p>

<pre><code>driver.mute()
</code></pre>

<p>is it possible with any other Webdrivers? like Firefox or ...?</p>

<p>Not sure if you can, generally for any page, do it after you have opened the page, but you can mute all the sound for the entire duration of the browser session by setting the <a href="http://peter.sh/experiments/chromium-command-line-switches/#mute-audio" rel="nofollow"><code>--mute-audio</code></a> switcher:</p>

<pre><code>from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--mute-audio")

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.youtube.com/watch?v=hdw1uKiTI5c")
</code></pre>

<hr>

<p>Or, you can <a href="http://stackoverflow.com/q/6376450/771848">mute the HTML5 video player directly</a>:</p>

<pre><code>video = driver.find_element_by_css_selector("video")
driver.execute_script("arguments[0].muted = true;", video)
</code></pre>

<p>You might need to add some delay before that to let the video be initialized before muting it. <code>time.sleep()</code> would not be the best way to do it - a better way is to subscribe to the <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events" rel="nofollow"><code>loadstart</code> media event</a> - the Python implementation can be found <a href="http://stackoverflow.com/a/28438996/771848">here</a>.</p>

<p>To summarize - complete implementation:</p>

<pre><code>from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

driver = webdriver.Chrome()
driver.set_script_timeout(10)

driver.get("https://www.youtube.com/watch?v=hdw1uKiTI5c")

# wait for video tag to show up
wait = WebDriverWait(driver, 5)
video = wait.until(EC.visibility_of_element_located((By.TAG_NAME, 'video')))

# wait for video to be initialized
driver.execute_async_script("""
    var video = arguments[0],
        callback = arguments[arguments.length - 1];

    video.addEventListener('loadstart', listener);

    function listener() {
        callback();
    };
""", video)

# mute the video
driver.execute_script("arguments[0].muted = true;", video)
</code></pre>

python selenium