1

I have been searching on the internet using Selenium (Java) interacting with Google Chrome Extension but have not been able to find an answer.

First Question Is there a way to launch the chrome extension since Selenium only interact with WebView but not on the chrome extensions button in the browser ?

I try this method "chrome-extension://id/index.html" but the extension did not launch as expected. I like find if there is another way to launch a chrome extension through selenium

Second Question I am trying to click on the elements in a chrome extension with Selenium webdriver. How do I do it ? I tried the driver.CurrentWindowHandle , but it does not detect the chrome extension.

Thanks

john
  • 413
  • 7
  • 16
  • Some Chrome extension can be operated with URL chrome-extension://id/popup.html. For example chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/popup.html. But it depends only on the extension itself wether it allow such option. In case you need edit some data in the extension, you can achieve it with direct LocalStorage edit using javascript. – pburgr Nov 15 '18 at 08:34
  • Thanks. I will do some readup . As far as I know , when i do that chrome-extension://id/popup.html , it is very different from clicking on the extension itself as the data are pulling from the server. I tried on a direct link (on what the extension was using) , there was some missing features. Some of UI Elements / scripts implement are on the extensions itself, but others are pulled from the server. Hence calling it did not work for me. – john Nov 16 '18 at 01:03

2 Answers2

3

Below is the solution with pyautogui (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
from Common_Methods.GenericMethods import *
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.

supputuri
  • 13,644
  • 2
  • 21
  • 39
0

Try to click on extension with this JsExecutor method:

driver.execute_script("window.postMessage('clicked_browser_action', '*')")
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26