如何在Selenium中禁用Chrome扩展程序

7

我正在尝试在启动selenium chrome时禁用所有chrome扩展程序。 但每次运行代码时,所有扩展程序都会启动。 有没有一种方法可以禁用这些扩展程序。

示例代码

public static void main(String[] args) throws IOException {
    System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability("chrome.binary", "C:\\Users\\ngzhongqin\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
    WebDriver driver = new ChromeDriver(capabilities);
    driver.get("http://www.cnn.com");
    WebElement searchBox = driver.findElement(By.name("q"));
}

 
3个回答

12

找到了解决方法。

  capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));

5

使用设置选项来替代chrome.switches并可行(Chrome版本53.0.2785.143 m,ChromeDriver 2.18.343845)。

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);

或者按照Chrome Driver文档的要求将选项设置为能力。

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);

ChromeDriver(capabilities)已被弃用


1
使用以下内容来设置 Chrome 选项:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接