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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值