通过硒webdriver打开铬扩展

通过硒webdriver打开铬扩展

问题描述:

我已经创建了一个Chrome扩展,使API调用数据库,并提取一些数据与当前打开的网站相关。例如,如果我打开target.com并点击扩展名,它会为您提供与target.com相关的数据。通过硒webdriver打开铬扩展

我想通过硒web驱动程序为它编写自动化测试,我可以定期运行回归测试。为了测试扩展名,我需要先打开扩展名(通常我们通过点击扩展名图标来完成)。

我尝试了不同的方式尝试点击扩展名图标,但没有成功。 (例如,使用键盘快捷键ALT - LEFT_ARROW - SPACE,但不能通过webdriver工作)。

我自己也尝试这种(自提here):

options = webdriver.ChromeOptions() 
options.add_argument("--app-id = mbopgmdnpcbohhpnfglgohlbhfongabi") 

但上面的代码不开放的扩展帮助。

我将不胜感激关于如何在Selenium Webdriver中使用python做到这一点。

试试这个:Running Selenium WebDriver using Python with extensions (.crx files)

使用

options.add_extension( 'path_to_extension.crx')

+0

问题是点击扩展名图标,没有使用扩展名选项加载浏览器。 – QualiT 2017-03-07 07:38:00

您可以将参数--load-extension通过选项所在的路径点传递到Chrome的webdriver到您的未打包的扩展。所以在你的情况下,你可以使用:options.add_argument("--load-extension=ABSOLUTE_PATH_TO_EXTENSION")

另外,here is链接到我写的一些代码,使用--load-extension方法。 (这是Ruby不是Python,但应该给你一些见解。)

+0

我已经能够使用'chrome_options.add_argument(“ - load-extension = ABSOLUTE_PATH_TO_UNPACKED_EXTENSION”)加载铬扩展;'但我的问题是我无法从webdriver点击扩展名(即打开扩展名) – curiousJ 2014-08-29 20:10:27

Selenium只支持与web视图的交互 - 所以这是不可能的。

我一直在寻找这个解决方案一段时间 - 事实证明,没有。

https://code.google.com/p/selenium/issues/detail?id=7805

http://grokbase.com/t/gg/selenium-developer-activity/148xndmkna/issue-7805-in-selenium-clicking-on-chrome-extension-to-open-popup

有同样的问题。通过使用链接解决它:chrome-extension://<the extension identity>/html/login.html - 而不是图标。 这样,我可以测试扩展的所有功能。

+0

虽然这是正确的,但与在弹出窗口中打开的行为有点不同。 – Xan 2015-12-11 10:17:38

+0

是否可以尝试subprocess.call和applescripts? – 2015-12-11 20:23:22

+0

@KathyChow你能提供更多详细信息吗?我会加载一个弹出窗口,点击Chrome扩展按钮 – Volatil3 2016-01-23 08:49:05

我们有类似的要求,使用Selenium WebDriver处理chrome附加组件。正如'@Aleksandar Popovic'所说,我们无法使用WebDriver单击Chrome扩展图标,因为图标不在网页中。

我们利用sikuli(利用图像识别的自动化工具)来点击Chrome加载项。之后,附加弹出窗口将成为另一个浏览器窗口,因此请使用切换窗口对附加弹出窗口执行操作。

下面是使用两个硒的webdriverSikuli爪哇的代码示例。

Sikuli将基于图像识别运行。在运行代码之前,chrome浏览器的屏幕截图并对其进行裁剪,以便只有Addon在图像中可用。将该图像保存为“AddonIcon.png”。

Sikuli将在屏幕上匹配该图像(在我们的例子中为AddonIcon.png)并模拟点击动作。

import java.io.File; 
import java.util.List; 
import java.util.Set; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.chrome.ChromeOptions; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 
import org.sikuli.script.App; 
import org.sikuli.script.FindFailed; 
import org.sikuli.script.Pattern; 
import org.sikuli.script.Screen; 
public class PageTest { 

    public static void main(String[] args) { 
     // Opening chrome with that addon 
     ChromeOptions options = new ChromeOptions(); 
     options.addExtensions(new File("Path to ur chrome addon (.cxt file)"));  
     System.setProperty("webdriver.chrome.driver", "path to chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(options); 
     driver.manage().window().maximize(); 

     // Creating object to the Sukali screen class 
     Screen s=new Screen(); 

     //Finding and clicking on the Addon image 
     try { 
      s.find("Path to the 'AddonIcon.png'"); 
      s.click("Path to the 'AddonIcon.png'"); 
     } catch (FindFailed e) {    
      e.printStackTrace(); 
     } 

     //Wait until new Addon popup is opened. 
     WebDriverWait wait = new WebDriverWait(driver, 5);  
     wait.until(ExpectedConditions.numberOfWindowsToBe(2)); 

     // Switch to the Addon Pop up 
     String parentWindow= driver.getWindowHandle(); 
     Set<String> allWindows = driver.getWindowHandles(); 
     for(String curWindow : allWindows){    
      if(!parentWindow.equals(curWindow)){ 
      driver.switchTo().window(curWindow); 
      } 
     } 

     /***********Ur code to work on Add-on popup************************/ 
    } 
} 

我希望这会帮助你。

from selenium import webdriver 
option = webdriver.ChromeOptions() 
option.add_extension("./testCRX/angular_ext.crx") 
driver = webdriver.Chrome(chrome_options=option) 
driver.get('chrome://extensions/') 

开放增加的扩展:我认为我们可以在Chrome设置shortkey://扩展/ - >键盘快捷键,键盘快捷键添加到您添加的扩展,然后发送Keyboard启动扩展。

+0

https://developer.chrome.com/extensions/commands为扩展添加快捷键 – 2017-06-05 07:21:35

我知道它不完全一样,但你不能注入一个iframe到你的弹出窗口的源代码页面,然后与它进行交互以进行测试吗?

<iframe src='chrome-extension://<extensionId>/popup.html'/>