我是靠谱客的博主 爱听歌白开水,最近开发中收集的这篇文章主要介绍python调用chrome插件_使用Python通过Selenium WebDriver打开Chrome扩展,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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.

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).

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.

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

解决方案

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.

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.

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

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 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 allWindows = driver.getWindowHandles();

for(String curWindow : allWindows){

if(!parentWindow.equals(curWindow)){

driver.switchTo().window(curWindow);

}

}

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

}

}

I hope this will help you.

最后

以上就是爱听歌白开水为你收集整理的python调用chrome插件_使用Python通过Selenium WebDriver打开Chrome扩展的全部内容,希望文章能够帮你解决python调用chrome插件_使用Python通过Selenium WebDriver打开Chrome扩展所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部