版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/galen2016/article/details/84308898
介绍:最近要做一个内嵌百度地图页面的自动化,由于selenium定位不到百度地图里面的元素,所以使用Sikuli以基于图片的方式来定位,这里对Sikuli的用法加以总结。
安装Sikuli
Sikuli官网介绍的是如何操作ppt,而我们是要定位当前页面的图片,实际使用的是SikuliX,所以Maven的配置不要复制官网的那个,在pom.xml添加如下依赖即可:
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>1.1.0</version>
</dependency>
工具类
我这里封装了一个工具类,封装了Sikuli的几个基本操作,如下:
import org.sikuli.script.FindFailed;
import org.sikuli.script.Image;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
/**
* Author: 灵枢
* Date: 2018/11/14
* Time: 16:07
* Description: Sikuli:可以根据图片来定位,然后执行操作
*/
public class SikuliUtil {
public static Pattern pattern = null;
public static Pattern pattern2 = null;
public static Screen screen = null;
/**
* 实现单击操作
* @param iconPath 图片路径
*/
public static void click(String iconPath){
if(isExist(iconPath)==false){
System.err.println("找不到图片:" + iconPath);
}
pattern = new Pattern(iconPath);
screen = new Screen();
try {
screen.click(pattern);
} catch (FindFailed e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
clear();
}
/**
* 双击操作
* @param iconPath 图片路径
* @return
*/
public static void doubleClick(String iconPath){
if(isExist(iconPath)==false){
System.err.println("找不到图片:" + iconPath);
}
pattern = new Pattern(iconPath);
screen = new Screen();
try {
screen.doubleClick(pattern);
} catch (FindFailed e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
clear();
}
/**
* 拖拽功能,拖动路径是icon1到icon2
* @param icon1Path
* @param icon2Path
*/
public static void dragDrop(String icon1Path,String icon2Path){
if(isExist(icon1Path)&&isExist(icon2Path)){
pattern = new Pattern(icon1Path);
pattern2 = new Pattern(icon2Path);
screen = new Screen();
try {
screen.dragDrop(pattern,pattern2);
} catch (FindFailed e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
clear();
}
}
/**
* 验证图片是否存在
* @param iconPath
*/
public static boolean isExist(String iconPath){
boolean exist;
pattern = new Pattern(iconPath);
screen = new Screen();
try {
screen.find(pattern);
exist = true;
} catch (FindFailed e) {
e.printStackTrace();
exist = false;
}
clear();
return exist;
}
/**
* 关闭弹窗
*/
public static void closePrompt(){
Screen.closePrompt();
}
/**
* 清空变量值
*/
public static void clear(){
if(pattern != null){
pattern = null;
}
if(pattern2 != null){
pattern2 = null;
}
if(screen != null){
screen = null;
}
}
}
实例
- 在百度地图页面,通过地图选点的方式查找线路:成都东站-成都大熊猫繁育研究基地
import org.openqa.selenium.chrome.ChromeDriver;
import test.java.utils.SikuliUtil;
import java.util.concurrent.TimeUnit;
/**
* Author: 灵枢
* Date: 2018/11/20
* Time: 19:46
* Description:在百度地图页面,通过地图选点的方式查找线路:成都东站-成都大熊猫繁育研究基地
*/
public class BMapTest {
public static void main(String[] args) throws InterruptedException {
//初始化driver
System.setProperty("webdriver.chrome.driver","resource/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
//进入百度地图页面
driver.get("https://map.baidu.com/");
driver.findElementById("sole-input").sendKeys("成都市");
driver.findElementById("search-button").click();
Thread.sleep(1000);
//使用Sikuli操作地图选点
SikuliUtil.click("resource/sikuliPicture/成都东站.png");
driver.findElementById("route-from").click();
SikuliUtil.click("resource/sikuliPicture/大熊猫繁育基地.png");
}
}
- 代码运行如下图:
- 有一点要注意的是,Sikuli对图片识别是针对电脑当前页面的,只有屏幕有对应的图片,才可能被定位到。
总结
优点:
- Sikuli可以操作地图、flash、视频等,只要是屏幕上出现的图片都可以,在一些selenium定位不到的地方使用Sikuli会有很好的效果
- 桌面的图片也可以定位到,如果有上传文件的操作就可以Sikuli定位
缺点:
- 对图片要求比较高,不能有遮挡,分辨率、色差可能也有影响
- 使用过多的图片导致项目太大