I'm building an automated test suite using Selenium Web Driver. At a certain point I must test how the page works by having a Chrome extension turn on or off. Think of it as you would want to click on the Adblock extension and then click disable for this site. Then, turn it on again. I searched all over the Internet and there is no way to implement this using just Selenium. Do you know how could I perform such an action? (from Java ideally)
-
Did you try to use [ChromeOptions](https://sites.google.com/a/chromium.org/chromedriver/extensions)? it allows you to set desired extensions for a run, so one test could use those and the next one - not. – ekostadinov Nov 01 '17 at 08:45
-
This is a really good tip! I could use it as a last resort. Ideally, I would like to turn the extension on/off by pressing the button and check how the site reacts. Thanks! ;) – RaresI Nov 01 '17 at 10:35
-
Turned the comment into answer so ppl can find it easily and vote if it is helpful. – ekostadinov Nov 01 '17 at 11:38
-
As we know, we cannot click on any browser extension using selenium webdriver, the best alternative is to use sikuli. Download the Jar file from "https://launchpad.net/sikuli/+download" Add to your library and follow the steps mentioned by piet.t. It worked for me.. – KUMARESH KARMAKAR Feb 19 '20 at 06:57
-
On blazemeter is a nice description available, that probably also works for you: [Blazemeter-way of testing crx extesion](https://www.blazemeter.com/blog/six-easy-steps-testing-your-chrome-extension-selenium) – Ernst Zwingli Oct 31 '17 at 18:54
3 Answers
One possible solution is to go with Chrome options and manage the extensions set to the WebDriver. Quick example:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
If you want to turn those On and OFF in a single test, you can spawn two separate drivers and compare the results, since I'm not sure that session reuse will do the job in this case.

- 6,880
- 3
- 29
- 47
Below is the solution is in Python with pyautogui (I believe it's similar to autoit in java - so you can extend the same solution for java also).
Pre-Condition:
save the extension image in the project folder (I saved it under "autogui_ref_snaps" folder in my example with "capture_full_screenshot.png" name
Python:
Imports needed
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import pyautogui #<== need this to click on extension
Script:
options = ChromeOptions()
options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
driver = webdriver.Chrome(
executable_path=os.path.join(chrome_options=options)
url = "https://google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(GenericMethods.get_full_path_to_folder('autogui_ref_snaps') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")
If you are loading an extension and it's not available in incognito mode then follow my answer in here to enable it.

- 13,644
- 2
- 21
- 39
Can use sikuli(GUI Automation tool) to click on browser addon.
Imports needed:
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
Script:
Pattern addon=new Pattern("D:\\My Files\\Addon.jpg"); //image of the addon must be given as a pattern for identifying that on the browser/webpage
Screen s=new Screen();
s.hover(addon);
s.click(addon);