How to click on a Chrome extension button with Selenium

前提是你 提交于 2020-01-06 07:12:55

问题


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.



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


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/51524109/how-to-click-on-a-chrome-extension-button-with-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!