使用Python通过Selenium WebDriver打开谷歌浏览器扩展

2024-05-02 23:41:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我正试图通过selenium web驱动程序为它编写自动化测试,我可以定期运行它进行回归测试。要测试扩展,我需要首先打开扩展(通常我们是通过单击扩展图标来完成的)。

我尝试了不同的方法试图单击扩展图标,但没有成功。(例如,使用键盘快捷键ALT-LEFT_ARROW-SPACE,但这在webdriver中不起作用)。

我也试过(提到过here):

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

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

如果您能在Selenium Webdriver中使用python,我将不胜感激。


Tags: 数据方法comapiweb数据库target网站
3条回答

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

我们有类似的需求,使用Selenium WebDriver开发chrome插件。正如“@Aleksandar Popovic”所说,我们不能用WebDriver点击chrome扩展图标,因为图标已经从网页中消失了。

我们使用sikuli(使用图像识别的自动化工具)来单击chrome插件。之后,加载项弹出窗口将是另一个浏览器窗口,因此使用切换窗口对加载项弹出窗口执行操作。

下面是Java中使用Selenium WebdriverSikuli的示例代码。

Sikuli将基于图像识别运行。在运行代码之前,chrome浏览器的屏幕截图并将其裁剪,以便在图像中只有插件可用。将该图像另存为“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************************/
    }
}

我希望这对你有帮助。

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

相关问题 更多 >