我们有类似的要求,使用Selenium WebDriver处理chrome附加组件.正如’@Aleksandar Popovic’所说,我们无法使用WebDriver点击chrome扩展图标,因为图标不在网页中.
我们使用sikuli(利用图像识别的自动化工具),点击chrome附加组件.之后,附加弹出窗口将是另一个浏览器窗口,因此使用切换窗口对附加组件弹出窗口执行操作.
以下是使用Selenium Webdriver和Sikuli的Java示例代码.
Sikuli将基于图像识别运行.在运行代码之前,Chrome浏览器的屏幕截图并将其裁剪,以便图像中只有Addon可用.将该图像另存为“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 allWindows = driver.getWindowHandles();
for(String curWindow : allWindows){
if(!parentWindow.equals(curWindow)){
driver.switchTo().window(curWindow);
}
}
/***********Ur code to work on Add-on popup************************/
}
}
我希望这能帮到您.