如何使用 Selenium 浏览器驱动程序关闭 Chrome 中的所有声音?
我们可以使用 Selenium webdriver 关闭 Chrome 中的所有声音。要关闭音频,我们必须为浏览器设置参数。对于 Chrome,我们将使用 ChromeOptions 类。
我们将创建 ChromeOptions 类的对象。然后使用该对象调用 addArguments 方法。然后将 −mute−audio 作为参数传递给该方法。最后,将此信息发送给驱动程序对象。
语法
ChromeOptions op = new ChromeOptions();
op.addArguments("−−mute−audio");
WebDriver d = new ChromeDriver(op);对于 Firefox,我们将使用 FirefoxOptions 类并为该类创建对象。然后使用该对象调用 addPreference 方法并将 media.volume_scale 和 0.0 作为参数传递给该方法。最后,将此信息发送给驱动程序对象。
语法
FirefoxOptions profile = new FirefoxOptions();
profile.addPreference("media.volume_scale", "0.0");
WebDriver driver = new FirefoxDriver(profile);示例
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();
}
}示例
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();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP