如何使用 Selenium WebDriver 在 Chrome 中激活 AdBlocker?

我想用 Selenium WebDriver Java 激活 Adblocker for Chrome.

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

怎么做?

推荐答案

你可以使用扩展的 crx 文件或者指向它的安装目录.假设你已经安装了它,你需要添加 ChromeOptions 到你的 new ChromeDriver:

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.

要查看您的扩展路径,请导航到 chrome://version 并查看配置文件路径".在该文件夹中查找扩展"文件夹.AdBlock Plus 的扩展 ID 是 cfhdojbkjhnklbpkdaibdccddilifddb.所以路径/到/扩展名应该类似于(Windows):

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>AppDataLocalGoogleChromeUser Data<profile>Extensionscfhdojbkjhnklbpkdaibdccddilifddb

相关文章