Selenium Webdriver——AutoIt和robot实现右键另存

方案如下:

1.selenium 弹出右键菜单

2.robot选择相关菜单

3.调用autoIt实现windows gui另存操作

 

test case 如下:

1.打开百度(谷歌浏览器)

2.选择百度图片,右键另存为

3.在弹出另存为窗口输入指定路径,单击保存


 

 

 

robot,模拟键盘操作,使用方向键 ↓

        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_DOWN);
          
         Thread.sleep(1000);

         robot.keyPress(KeyEvent.VK_DOWN);
          
         Thread.sleep(1000);
          
        robot.keyPress(KeyEvent.VK_ENTER);

 autoIt处理另存为窗口

定位保存按钮,使用ControlFocus方法,用于识别Window 窗口。

WinWait() 设置10 秒钟用于等待窗口的显示

定位编辑框(文件名)title是“另存为”,class是Edit ,instance 是1 

使用ControlSetText方法输入保存路径,定位保存按钮,使用ControlClick方法单击保存按钮

 1 ;ControlFocus("title","text",controlID) Edit1=Edit instance 1
 2 ControlFocus("另存为", "","Edit1")
 3 
 4 ; Wait 10 seconds for the Upload window to appear
 5 WinWait("[CLASS:#32770]","",10)
 6 
 7 Sleep(2000)
 8 
 9 ; Set the File name text on the Edit field
10 ControlSetText("另存为","", "Edit1", "D:\save_file.png")
11 
12 Sleep(5000)
13 
14 ; Click on the Open button
15 ControlClick("另存为", "","Button1");

然后使用autoIt转换为EXE格式的可执行文件

使用java的runTime类调用

Runtime.getRuntime().exec("D:\\savePicture.exe");

 全部代码如下(打开百度,右键保存百度图片)

 1 import java.awt.AWTException;
 2 import java.awt.Robot;
 3 import java.awt.event.KeyEvent;
 4 
 5 import org.openqa.selenium.By;
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.WebElement;
 8 import org.openqa.selenium.chrome.ChromeDriver;
 9 import org.openqa.selenium.interactions.Actions;
10 
11 public class savepicture {
12 
13     public static void main(String[] args) throws InterruptedException, AWTException{
14         // TODO Auto-generated method stub
15         WebDriver driver = new ChromeDriver();
16         driver.get("http://www.baidu.com");
17         Actions actions=new Actions(driver);
18         WebElement baidupiuture =driver.findElement(By.xpath(".//*[@id='lg']/img"));
19         actions.moveToElement(baidupiuture).contextClick().build().perform();
20 
21         Robot robot = new Robot();
22         robot.keyPress(KeyEvent.VK_DOWN);
23           
24          Thread.sleep(1000);
25 
26          robot.keyPress(KeyEvent.VK_DOWN);
27           
28          Thread.sleep(1000);
29           
30         robot.keyPress(KeyEvent.VK_ENTER);
31           
32         
33         Runtime rn = Runtime.getRuntime();
34         try{
35             String str = "D:\\savePicture.exe" ;
36             rn.exec(str);
37             
38             } catch (Exception e){
39             System.out.println("Error to run the exe");
40             }
41         Thread.sleep(10000);
42         driver.quit();        
43     }
44 }

 

posted @ 2016-08-05 11:35  hjhsysu  阅读(831)  评论(0编辑  收藏  举报