how to disable chrome extension in selenium

2019-01-28 03:32发布

问题:

I am trying to disable all chrome extensions when starting up my selenium chrome. But all extensions keep starting up each time I run the code. Is there a way of disabling the extensions.

Sample Code

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"));
}

回答1:

Found a fix.

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


回答2:

Setting capability chrome.switches did not work for me (Chrome Version 53.0.2785.143 m, ChromeDriver 2.18.343845)

Instead using options works:

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

or as per Chrome Driver documentation to set options as capability

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

ChromeDriver(capabilities) is deprecated



回答3:

Use the following to set chrome options:

ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");