How to mute all sounds in chrome webdriver with selenium?


We can mute all sounds in Chrome with Selenium webdriver. To mute the audio we have to set parameters for the browser. For Chrome, we shall use the ChromeOptions class.

We shall create an object of the ChromeOptions class. Then utilize that object to invoke the addArguments method. Then pass −mute−audio as a parameter to that method. Finally, send this information to the driver object.

Syntax

ChromeOptions op = new ChromeOptions();
op.addArguments("−−mute−audio");
WebDriver d = new ChromeDriver(op);

For Firefox, we shall use the FirefoxOptions class and create an object for that class. Then utilize that object to invoke the addPreference method and pass media.volume_scale and 0.0 as parameters to the method. Finally, send this information to the driver object.

Syntax

FirefoxOptions profile = new FirefoxOptions();
profile.addPreference("media.volume_scale", "0.0");
WebDriver driver = new FirefoxDriver(profile);

Example

Code Implementation for Chrome.

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class AudioMuteChrome {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      // object of ChromeOptions class
      ChromeOptions op = new ChromeOptions();
      // add muted argument
      op.addArguments("−−mute−audio");
      // adding options to browser
      ChromeDriver driver= new ChromeDriver(op);
      driver.get("https://www.youtube.com/watch?v=WV40Rb1J−AI/");
      driver.quit();
   }
}

Example

Code Implementation for Firefox.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class MuteAudioFirefox{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
      "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      // instance of FirefoxOptions class
      FirefoxOptions profile = new FirefoxOptions();
      // adding mute browser preferences
      profile.addPreference("media.volume_scale", "0.0");
      WebDriver driver = new FirefoxDriver(profile);
      driver.get("https://www.youtube.com/watch?v=WV40Rb1J−AI/");
      driver.quit();
   }
}

Updated on: 30-Jan-2021

875 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements