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 @   hjhsysu  阅读(845)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示