DoctorEvil
DoctorEvil

Reputation: 473

How to click on a Chrome extension button with Selenium

I'm wondering if there is a way to click on an extension button with Selenium without using win32api. Here's the image, in the black circle is the extension icon which I'd want to click.

enter image description here

That icon is located in the toolbar, so I don' know if there's a way to look for it with Selenium.

Upvotes: 6

Views: 14469

Answers (5)

Umang Mishra
Umang Mishra

Reputation: 11

i am facing the same problem but i am able to do it with sikuli. its very easy to use this and you can find the jar in maven repository for this

Upvotes: 0

therealak12
therealak12

Reputation: 1316

First install pyautogui then find an image of the extension icon and save it on your computer in some location like ./extn_icon.png. Then use the following code to click on the extension:

import pyautogui

img_location = pyautogui.locateOnScreen(image_path, confidence=0.5)
image_location_point = pyautogui.center(img_location)
x, y = image_location_point
pyautogui.click(x, y)

Upvotes: 4

supputuri
supputuri

Reputation: 14135

Below is the solution is in Python with pyautogui.

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

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.

Upvotes: 1

sshah98
sshah98

Reputation: 352

You could work around this by using this link with selenium. The behavior might be slightly different, however.

chrome-extension://<the extension identity>/html/login.html

Visit this link to get extension identity: How to get extension identity

Upvotes: 0

theGuy
theGuy

Reputation: 693

No, It is not possible with Selenium. The only way to achieve what you want to do is to use any automation tool that actually runs directly in the OS that you use.

Upvotes: 1

Related Questions