上一次寫右鍵下載是結合robot,這次是使用selenium+sikuli
上一次日志:http://www.cnblogs.com/tobecrazy/p/3969390.html
有關sikuli的介紹,和簡單使用請參考:http://www.cnblogs.com/tobecrazy/p/4516369.html
關於sikuli的缺點:
1.運行腳本時候,必須截圖,圖片比較占用系統空間
2.腳本執行過程中,不能移動鼠標,而selenium可以最小化,任意移動鼠標
話不多說,介紹一下場景:
1.打開百度
2.右鍵單擊百度logo
3.點擊彈出菜單的save as
4. 輸入保存位置和文件名
5. 點擊保存菜單
首先要做的是,截圖,截圖保存位置c:/selenium
腳本如下:
//create chrome webDriver with parameter ChromeOptions options = new ChromeOptions(); options.addArguments("--test-type"); Screen screen=new Screen(); System.setProperty("webdriver.chrome.driver", "C:\\webdriver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(options); String login_url="https://www.baidu.com/"; driver.get(login_url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS); //find baidu logo WebElement baiduLogo=driver.findElement(By.xpath("//div[@id='lg']/img")); Actions actions = new Actions(driver); //right click actions.contextClick(baiduLogo).perform(); Pattern saveAs =new Pattern("c:\\selenium\\saveAs.png"); screen.click(saveAs); Pattern inputBox1 =new Pattern("c:\\selenium\\inputBox1.png"); screen.find(inputBox1); screen.type("c:\\selenium\\baiduLogo.png"); Pattern saveButton =new Pattern("c:\\selenium\\saveButton.png"); screen.click(saveButton); //wait for 60s to verify download success or not try { Thread.sleep(60000); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { File f=new File("c:\\selenium\\baiduLogo.png"); if(f.exists()) { System.out.println("PASS"); } driver.close(); }