Sikuli+Selenium查詢百度地圖線路

介紹:最近要做一個內嵌百度地圖頁面的自動化,由於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對圖片識別是針對電腦當前頁面的,只有屏幕有對應的圖片,纔可能被定位到。

總結

優點:

  1. Sikuli可以操作地圖、flash、視頻等,只要是屏幕上出現的圖片都可以,在一些selenium定位不到的地方使用Sikuli會有很好的效果
  2. 桌面的圖片也可以定位到,如果有上傳文件的操作就可以Sikuli定位

缺點:

  1. 對圖片要求比較高,不能有遮擋,分辨率、色差可能也有影響
  2. 使用過多的圖片導致項目太大
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章