使用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!");
		}
		
		
	
	}
}

  

 

使用selenium进行右键保存图片,可以按照以下步骤进行操作: 1. 首先,导入必要的包和模块,可以使用以下代码引入selenium的ActionChains和Keys类: ```python from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys ``` 2. 获取图片的预览链接,可以使用正则表达式或xpath等方式获取链接。 ```python for i in res: browser.get(i) # 进入链接 time.sleep(3) pic = browser.find_element_by_xpath('//*[@id="wallpaper"]') # 获取元素 ``` 3. 使用ActionChains模拟鼠标操作,将鼠标移动到图片上方并右键点击。 ```python action = ActionChains(browser).move_to_element(pic) # 移动到该元素 action.context_click(pic) # 右键点击该元素 action.perform() # 执行 ``` 4. 使用pyautogui模块模拟键盘操作,按下键盘上的V键来保存图片。 ```python pyautogui.typewrite(['v']) # 敲击V进行保存 time.sleep(1) # 单击图片另存之后等待1秒 pyautogui.typewrite(['enter']) # 敲击回车键 ``` 以上是使用selenium进行右键保存图片的方法,通过模拟鼠标和键盘操作来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Selenium操作实现鼠标右键保存图片](https://blog.csdn.net/freesigefei/article/details/52213017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Selenium入门,最近看到的都师一些小白想学测试,今天就分享入门吧~](https://blog.csdn.net/weixin_49346599/article/details/108286979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值