使用 Python 通过 Selenium WebDriver 打开 chrome 扩展 [英] Open a chrome extension through Selenium WebDriver using Python

查看:38
本文介绍了使用 Python 通过 Selenium WebDriver 打开 chrome 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 chrome 扩展,它可以对数据库进行 API 调用并获取一些与当前打开的网站相关的数据.例如,如果我打开 target.com 并单击扩展程序,它将为您提供与 target.com 相关的数据.

I have created a chrome extension that makes API calls to database and fetches some data relevant to a website that is currently open. For example, if I open target.com and click on extension that it will give you data relevant to target.com.

我正在尝试通过 selenium Web 驱动程序为其编写自动化测试,我可以定期运行该驱动程序以进行回归测试.要测试扩展程序,我需要先打开扩展程序(通常我们通过单击扩展程序图标来完成).

I am trying to write automated tests for it through selenium web driver which I can run on a regular basis for regression testing. To test the extension, I need to first open the extension (generally we do it by clicking on the extension icon).

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

I have tried different ways of attempting to click on the extension icon but have not been successful. (For example, using the keyboard shortcut ALT - LEFT_ARROW - SPACE but that does not work through webdriver).

我也试过这个(在这里提到):

I have also tried this (mentioned here):

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

但是上面的代码对打开扩展没有帮助.

But above code does not help in opening extension.

如果您对如何在 Selenium Webdriver 中使用 python 执行此操作有任何想法,我将不胜感激.

I would appreciate any thoughts on how can I do this using python in Selenium Webdriver.

推荐答案

我们有类似的需求,正在使用 Selenium WebDriver 开发 chrome 插件.正如@Aleksandar Popovic"所说,我们无法使用 WebDriver 单击 chrome 扩展图标,因为图标不在网页中.

We have similar requirement, working on chrome add-on with Selenium WebDriver. As '@Aleksandar Popovic' said, we can't click chrome extension icon with WebDriver, as icon is out of the Webpage.

我们利用sikuli(利用图像识别的自动化工具),点击chrome添加-在.在该附加弹出窗口之后将是另一个浏览器窗口,因此请使用切换窗口对附加弹出窗口执行操作.

We make use of sikuli (Automation tool that make use of image recognition), to click the chrome add-on. After that add-on popup will be another browser window, so use switch window to perform actions on add-on popup.

这是使用 Selenium WebdriverSikuliJava 示例代码.

Here is the sample code in Java using both Selenium Webdriver and Sikuli.

Sikuli 将基于图像识别运行.在运行代码之前,Chrome 浏览器的屏幕截图并进行裁剪,以便图像中只有插件可用.将该图像另存为AddonIcon.png".

Sikuli will run based on image recognition. Before running the code, the screen shot of the chrome browser and crop it so that only Addon is available in the image. Save that image as "AddonIcon.png".

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

Sikuli will match that image (In our case AddonIcon.png ) on screen and simulate the click action on it.

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************************/
    }
}

希望对你有所帮助.

这篇关于使用 Python 通过 Selenium WebDriver 打开 chrome 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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