首先感谢Lakshay Sharma 大神的指导

最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图

如果我想右键另存为,根本操作不了。

也有在网上看到webdriver right click option的一些代码,拿来用发现不能用的。

Actions act = new Actions(driver);

WebElement link = driver.findElement(By.id("xpath"));

act.moveToElement(link).contextClick().sendKeys(Keys.ArrowsDown).build().perform();

使用Actions没办法拿到右键菜单。

后来在某论坛发帖,一个印度籍的专家给出solution, perfect!完美解决

http://forumsqa.com/question/how-to-click-the-option-of-the-menu-which-the-right-click-pop-up/

方案如下:

1.selenium 弹出右键菜单

2.robot选择相关菜单

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

tips:

目测autoIt没法操作web elements,比如我当前使用autoIt获取富文本框,却没法拿到相关的 classs,拿到的只能是浏览器的信息

废话不多说,test case 如下

1.打开autoIt的官网

2.click download 页面

3.选择autoIt下载图标,单击右键另存为

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

如果您有selenium基础,1~2都很easy。 调出右键菜单只需要action的contexClick方法

Action.contextClick(myElement).build().perform();

接下来就是选择右键菜单的另存为

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

Robot robot = new Robot();

// This will bring the selection down one by one

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);

// This is to release the down key, before this enter will not work

robot.keyRelease(KeyEvent.VK_DOWN);

Thread.sleep(1000);

robot.keyPress(KeyEvent.VK_ENTER);

接下来就该交给autoIt处理另存为窗口

autoIt使用方法:

依次定位保存按钮,使用ControlFocus方法,定位编辑框(文件名)title是“另存为”,class是Edit ,instance 是1

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

ControlFocus("另存为", "","Edit1");ControlFocus("title","text",controlID) Edit1=Edit instance
; Wait seconds for the Upload window to appear WinWait("[CLASS:#32770]","",) ; Set input focus to the edit control of Upload window using the handle returned by WinWait ControlFocus("另存为","","Edit1") Sleep() ; Set the File name text on the Edit field ControlSetText("另存为", "", "Edit1", "d:\autoit-v3-setup") Sleep() ; Click on the Open button ControlClick("另存为", "","Button1");

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

使用java的runTime类调用

Runtime.getRuntime().exec("E:\\test\\download.exe");

全部代码如下:

package com.packt.webdriver.chapter2;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException; import java.util.List;
import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions; import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium; public class AutoItDownload { public static void main (String [] args) throws InterruptedException, AWTException
{ String URL="https://www.autoitscript.com";
//avoid Chrome warnning message like "unsupported command-line flag --ignore-certificate-errors. "
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type"); System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
//WebDriver driver = new FirefoxDriver(); driver.get(URL); driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement editor=driver.findElement(By.xpath("//*[@id='menu-item-207']"));
Actions actions=new Actions(driver);
actions.moveToElement(editor).perform();
//locate download link
WebElement d=driver.findElement(By.xpath("//*[@id='menu-item-209']/a"));
d.click(); Thread.sleep(5000);
//right click the download link //locate download link //right click the download link
WebElement download=driver.findElement(By.xpath("//img[starts-with(@alt,'download autoit')]"));//*[@id="content-area"]/div/table/tbody/tr[1]/td[2]/p/a/img
JavascriptExecutor js=(JavascriptExecutor)driver;
// roll down and keep the element to the center of browser
js.executeScript("arguments[0].scrollIntoView(true);", download);
actions.moveToElement(download).contextClick().build().perform();
Robot robot = new Robot(); // This will bring the selection down one by one 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); // This is to release the down key, before this enter will not work robot.keyRelease(KeyEvent.VK_DOWN); Thread.sleep(1000); robot.keyPress(KeyEvent.VK_ENTER); // this code block will snapshot the browser
File scrShot=new File("d:\\1.png");
File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try { FileUtils.copyFile(scrFile, scrShot);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{ System.out.println("screen shot finished");
}
// System.out.println(scrFile.getAbsolutePath()); //call autoIt to save the file
try {
Runtime.getRuntime().exec("E:\\test\\download.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Thread.sleep(150000);
driver.quit(); } }

效果图:

selenium webdriver 右键另存为下载文件(结合robot and autoIt)的更多相关文章

  1. webdriver高级应用- 右键另存为下载文件

    1.要使用右键另存,需要先按照第三方工具AutoIt: 链接: https://pan.baidu.com/s/12aBBhOOTmyQpH9hukt0XGA 密码: fcdk 2.创建一个名为loa ...

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

    1.需要借住autoit工具和Robot类,下载地址:https://www.autoitscript.com/site/autoit/downloads/ 2.autoit的使用不再详细讲解.如下图 ...

  3. 转:python webdriver API 之下载文件

    webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中.要想下载文件,首选要先确定你所要下载的文件的类型.要识别自动文件的下载类型可以使用 curl ,如图 ...

  4. python3+selenium入门14-上传下载文件

    上传文件一种方式是通过定位input标签,然后使用send_keys()方法传入需要上传文件的路径.另一种是使用第三方插件去上传文件.下面看下imput标签的方式.工具可以自己查下. <!DOC ...

  5. js使用浏览器的另存为下载文件

    页面上的页面如下: 我需要根据返回的url下载文件: js: //判断浏览器类型 function myBrowser(){ var userAgent = navigator.userAgent; ...

  6. python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

    0.关键实现:程序窗口前置 python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作 1.参考 aut ...

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

    方案如下: 1.selenium 弹出右键菜单 2.robot选择相关菜单 3.调用autoIt实现windows gui另存操作 test case 如下: 1.打开百度(谷歌浏览器) 2.选择百度 ...

  8. Win 7 IE11不能下载文件,右键另存为也不行

    在IE11中不能下载文件,右键另存为也无效. 发现 在IE11中点击“INTERNET选项”后,IE临时文件夹的地址没有显示,大小为0,修改只能让设置在8-8MB,注销再登录后,一切设置无效. 问题就 ...

  9. python webdriver api-右键另存下载文件

    右键另存下载文件 先编辑SciTE脚本: ;ControlFocus("title","text",controlID) ;表示将焦点切换到标题为title窗体 ...

随机推荐

  1. [fiddler] 使用fiddler script自定义代理规则

    场景 fiddler作为调试代理工具,可以捕获电脑与互联网之间所有http通讯. 通过可视化操作或命令行可以按某些规则截获特定请求并修改,但当我们需要批量对请求进行更复杂的逻辑操作时,则不是很方便. ...

  2. 给织梦添加英文栏目标题在chanel标签中调用

    网上很多添加英文栏目标题的方法,大家自己去百度一下就好,但是修改之后在chanel标签中是调用不了的,那么解决办法如下: 想要在channel 中使用,例如: {dede:channel type=' ...

  3. Rabbitmq集群升级方案

    升级Rabbitmq 3.6.3版本至3.6.6版本,升级过程中的一些关键步骤记录 Step 1: 顺序关闭集群所有节点,这里注意最后一个关闭的节点必须保证为硬盘节点,而非RAM节点: centOS ...

  4. 2016总结&2017计划

    2016总结 总体而言,上半年比较勤快,下半年偷懒了.下半年仔细看了Barfoot的书<state estimation for robotics>,收获很大. 2017计划 目前打算写以 ...

  5. [个人论文]一种基于GPU并行计算的MD5密码解密方法

    求轻喷... [顺便get一份LaTeX论文模板....还是XeLaTex好用.珍爱生命远离CJK http://files.cnblogs.com/files/pdev/paper.zip

  6. Zabbix协议分析

    概述 Zabbix使用一种自定义的基于TCP的协议与客户端进行通信 Zabbix <- TCP -> Zabbix agent 协议内容 <HEADER> - bytes) & ...

  7. django的cookie和session以及内置信号、缓存

    cookie和session cookie和session的作用: cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话.两者最大的区别是cookie的信息是存放在浏览 ...

  8. substr()函数

    substr 定义于头文件 <string> string substr (size_t pos = 0, size_t len = npos) const;复制子字符串,要求从指定位置开 ...

  9. vim简单使用

    摘自:http://blog.csdn.net/niushuai666/article/details/7275406   一.安装vim   sudo apt-get install vim   二 ...

  10. POJ1742Coins(并不理解是什么意思)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32309   Accepted: 10986 Descripti ...