Sikuli+Selenium查詢百度地圖線路
阿新 • • 發佈:2018-12-01
介紹:最近要做一個內嵌百度地圖頁面的自動化,由於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定位
缺點:
- 對圖片要求比較高,不能有遮擋,解析度、色差可能也有影響
- 使用過多的圖片導致專案太大