使用selenium实现右键另存为保存文件

1.需要借住autoit工具和Robot类,下载地址:https://www.autoitscript.com/site/autoit/downloads/

2.autoit的使用不再详细讲解。如下图:

3.识别到保存窗口后,用autoit编辑器编写脚本

1
2
3
4
5
6
7
8
ControlFocus("保存图片", "","Edit1")
;ControlFocus("title","text",controlID) Edit1=Edit instance 1
 
WinWait("[CLASS:#32770]","",10)
 
ControlSetText("保存图片", "", "Edit1", "d:\autoit-v3-setup1.png")
Sleep(2000)
ControlClick("保存图片", "","Button1");

 保存为au3文件,再用D:\autoit3\Aut2Exe\Aut2exe转为为exe,在java程序中调用

4.代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.gmsd;
 
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
/**
 * @author QiaoJiaofei
 * @version 创建时间:2015年7月1日 下午5:00:31
 * 类说明
 */
public class Testyanzm {
    WebDriver dr = null;
     
    @Test
    public void testf() throws AWTException, InterruptedException {
        System.setProperty("webdriver.ie.driver", "D:/BaiduYunDownload/selenium/IEDriverServer.exe");
        dr = new InternetExplorerDriver();
        dr.manage().window().maximize();
        dr.get("http://172.16.30.209:5555/register.shtml");
        Actions action = new Actions(dr);
        action.contextClick();// 鼠标右键在当前停留的位置做单击操作
        action.contextClick(dr.findElement(By.id("codeimg"))).build().perform();
         
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
         
        robot.keyRelease(KeyEvent.VK_DOWN);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_ENTER);     
        Thread.sleep(5000);
         
        Runtime rn = Runtime.getRuntime();
        Process p = null;
        try {          
            p = rn.exec("\"D:/autoitexe/rightsave.exe\"");
        } catch (Exception e) {
            System.out.println("Error exec!");
        }
         
         
     
    }
}

  

 

posted on   乔叶叶  阅读(7965)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示