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

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

问题描述

我正在使用 Selenium Web Driver 构建一个自动化测试套件.在某个时候,我必须通过打开或关闭 Chrome 扩展程序来测试页面的工作方式.把它想象成你想要点击 Adblock 扩展,然后点击这个站点的禁用.然后,再次打开它.我在互联网上进行了搜索,仅使用 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).

前置条件:

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

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:UserssupputuriAppDataLocalGoogleChromeUser DataDefaultExtensionsfdpohaocaechififmbbbbbknoalclacl5.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天全站免登陆