如何触发Chrome扩展程序按钮的点击? [英] How to trigger a click on a chrome extension button?

查看:1121
本文介绍了如何触发Chrome扩展程序按钮的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Web驱动程序构建一个自动化测试套件.在某些时候,我必须通过打开或关闭 Chrome扩展程序来测试页面的工作方式.可以将其视为您想要单击Adblock扩展名,然后单击此站点的禁用"功能.然后,再次打开它. 我在Internet上进行了搜索,没有办法仅使用Selenium 来实现. 您知道我该怎么做吗? (最好是Java)

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)

推荐答案

以下是pyautogui在Python中的解决方案(我相信它与Java中的 autoit 类似-因此您可以扩展相同的解决方案也是 java ).

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).

前提条件:

将扩展程序图像保存在项目文件夹中(我在示例中以"capture_full_screenshot.png"名称将其保存在"autogui_ref_snaps"文件夹下

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:

需要导入

from selenium import webdriver
from selenium.webdriver import ChromeOptions
import pyautogui  #<== need this to click on extension

脚本:

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.

这篇关于如何触发Chrome扩展程序按钮的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆