使用Python通过Selenium WebDriver打开Chrome扩展程序

12

我创建了一个Chrome扩展程序,它通过API调用数据库并获取与当前打开的网站相关的数据。例如,如果我打开target.com并点击扩展程序,它将提供与target.com相关的数据。

我正在尝试通过Selenium Web Driver编写自动化测试用例,以便可以定期运行回归测试。为了测试该扩展程序,我需要首先打开扩展程序(通常是通过单击扩展程序图标来完成)。

我已经尝试了不同的方式来尝试单击扩展程序图标,但一直没有成功。(例如,使用键盘快捷键ALT-LEFT_ARROW-SPACE,但这在Web Driver中不起作用)。

我还尝试过以下方法(在此处提到:https://developer.chrome.com/apps/first_app):

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

但是上述代码不能帮助打开扩展程序。

我希望能听到任何有关如何在Selenium Webdriver中使用Python实现此功能的想法。

8个回答

4
我们有类似的需求,正在使用Selenium WebDriver开发Chrome插件。正如“@Aleksandar Popovic”所说,我们无法使用WebDriver单击Chrome扩展程序图标,因为该图标不在网页中。
我们利用sikuli(一种利用图像识别的自动化工具)来单击Chrome插件。之后,插件弹出窗口将成为另一个浏览器窗口,因此需要切换窗口以对插件弹出窗口执行操作。
下面是使用Java编写的示例代码,同时使用了Selenium 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************************/
    }
}

我希望这可以帮助你。


4

我曾经也遇到过同样的问题。通过使用链接:chrome-extension://<扩展标识>/html/login.html,而不是图标,我解决了这个问题。这样,我可以测试扩展的所有功能。


2
当为真时,行为与在弹出窗口中打开略有不同。 - Xan
能否尝试使用subprocess.call和applescripts? - Kathy Chow
@KathyChow,你能提供更多细节吗?我需要在点击Chrome扩展按钮时加载一个弹出窗口。 - Volatil3
1
@Volatil3 是的,我也需要在点击Chrome扩展按钮时加载弹出窗口。但有时使用链接:chrome-extension://<扩展标识符>/html更加有用。 - Kathy Chow
1
大多数扩展程序,我想,都与当前活动选项卡交互 - 在这种情况下,这并没有真正帮助,因为扩展程序也成为了活动选项卡? - oatsoda

4

1
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://extensions/ -> Keyboard shortcuts中设置快捷键,将快捷键添加到您添加的扩展中,然后发送Keyboard以启动该扩展。


https://developer.chrome.com/extensions/commands 为扩展添加快捷键 - Bao Jie
3
你成功证明这个有效吗?我能看出这可能会让你到达"弹出窗口"的界面,但是Selenium是否能够访问其中显示的HTML页面并自动化操作它呢? - oatsoda

1
你可以使用此解决方案来模拟点击扩展程序图标。
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.postMessage('clicked_browser_action', '*')");

0

我知道这不完全相同,但你可以将一个带有弹出窗口源的 iframe 注入页面中,然后与之交互进行测试吗?

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

-3

问题在于点击扩展图标,而不是加载带有扩展选项的浏览器。 - QualiT

-3

您可以通过选项将参数--load-extension传递给Chrome webdriver,其中路径指向您的未打包扩展。因此,在您的情况下,您可以使用:options.add_argument("--load-extension=ABSOLUTE_PATH_TO_EXTENSION")

另外,这里是我编写的一些代码的链接,它使用--load-extension方法工作。(它是Ruby而不是Python,但应该能为您提供一些见解。)


我已经成功使用 chrome_options.add_argument("--load-extension=ABSOLUTE_PATH_TO_UNPACKED_EXTENSION"); 加载了 Chrome 扩展,但我的问题是我无法通过 webdriver 点击扩展(即打开扩展)。 - curiousJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接