How to Activate AdBlocker in Chrome using Selenium WebDriver?

user2145488 picture user2145488 · Feb 14, 2017 · Viewed 8.4k times · Source

I would like to activate Adblocker for Chrome with Selenium WebDriver Java.

How can it be done?

Answer

Moshisho picture Moshisho · Feb 14, 2017

You can either use the crx file of the extension or point to a directory it's installed in. Assuming you have it installed, you'll need to add ChromeOptions to your new ChromeDriver:

ChromeOptions options = new ChromeOptions();
options.addArguments("load-extension=/path/to/extension");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

See here for more details and walkthrough.

EDIT: to see your extensions path, navigate to chrome://version and look at 'Profile Path'. In that folder look for 'Extensions' folder. The extension ID for AdBlock Plus is cfhdojbkjhnklbpkdaibdccddilifddb. So the path/to/extension should be something like (Windows):

C:\Users\<user>\AppData\Local\Google\Chrome\User Data\<profile>\Extensions\cfhdojbkjhnklbpkdaibdccddilifddb