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

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

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

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

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.代码如下

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 2015-07-15 18:06  乔叶叶  阅读(7889)  评论(0编辑  收藏  举报

导航