点击带有硒元素的Chrome扩展程序中的元素 [英] Click on elements in Chrome Extension with selenium

查看:75
本文介绍了点击带有硒元素的Chrome扩展程序中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Selenium(Java)与Google Chrome扩展程序进行交互,但无法找到答案.

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

第一个问题 因为Selenium仅与WebView交互而不在浏览器的chrome扩展按钮上交互,所以有没有办法启动chrome扩展?

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 ?

我尝试这种方法 "chrome-extension://id/index.html",但是扩展未按预期启动.我喜欢发现是否还有另一种通过硒启动chrome扩展的方法

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

第二个问题 我正在尝试使用Selenium webdriver单击chrome扩展中的元素.我该怎么做 ?我尝试了driver.CurrentWindowHandle,但是它没有检测到chrome扩展名.

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.

谢谢

推荐答案

下面是pyautogui的解决方案(类似于Java中的 autoit -因此您也可以为 java扩展相同的解决方案).

Below is the solution with pyautogui (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
from Common_Methods.GenericMethods import *
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天全站免登陆