使用Python通过Selenium WebDriver打开chrome扩展

cur*_*usJ 9 python selenium google-chrome google-chrome-extension selenium-webdriver

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

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

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

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

options = webdriver.ChromeOptions()
options.add_argument("--app-id = mbopgmdnpcbohhpnfglgohlbhfongabi")
Run Code Online (Sandbox Code Playgroud)

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

我很感激有关如何在Selenium Webdriver中使用python执行此操作的任何想法.

Ven*_*nta 5

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

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

这是同时使用Selenium WebdriverSikuli 的Java示例代码。

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************************/
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您。


小智 -3

试试这个:使用带扩展名的 Python(.crx 文件)运行 Selenium WebDriver

使用

options.add_extension('path_to_extension.crx')