感谢http://www.cnblogs.com/tobecrazy/p/3969390.html  博友的分享

最近在学习selenium的一些鼠标的相关操作

自己在百度的相关操作代码

  /**     * Selenium Keys键盘按键包使用实例键盘操作     */ @Test public void RightClickTest() throws Exception {//右击和左键双击操作 driver.get("http://www.baidu.com"); WebElement element = driver.findElement(By.id("su")); //右键操作用到Action类 Actions actions=new Actions(driver); actions.contextClick(element).perform(); //右击哪个元素,如果不传的话默认左上角元素// actions.doubleClick(element).build().perform(); //左键双击,如果不写build也是可以的 //选择右侧的菜单,选择的也是另存为 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_ENTER); }

如下为对方原文

selenium webdriver 右键另存为下载文件(结合robot and autoIt)

最近一直在研究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 onerobot.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 workrobot.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 1
; Wait 10 seconds for the Upload window to appearWinWait("[CLASS:#32770]","",10); Set input focus to the edit control of Upload window using the handle returned by WinWaitControlFocus("另存为","","Edit1")Sleep(2000); Set the File name text on the Edit fieldControlSetText("另存为", "", "Edit1", "d:\autoit-v3-setup")Sleep(2000); Click on the Open buttonControlClick("另存为", "","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 linkWebElement 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 linkWebElement download=driver.findElement(By.xpath("//img[starts-with(@alt,'download autoit')]"));//*[@id="content-area"]/div/table/tbody/tr[1]/td[2]/p/a/imgJavascriptExecutor js=(JavascriptExecutor)driver;// roll down and keep the element to the center of browserjs.executeScript("arguments[0].scrollIntoView(true);", download);actions.moveToElement(download).contextClick().build().perform();Robot robot = new Robot();// This will bring the selection down one by onerobot.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 workrobot.keyRelease(KeyEvent.VK_DOWN);Thread.sleep(1000);robot.keyPress(KeyEvent.VK_ENTER);// this code block will snapshot the browserFile 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 blockSystem.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 filetry {Runtime.getRuntime().exec("E:\\test\\download.exe");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Thread.sleep(150000);driver.quit();}}

效果图:

转载于:https://www.cnblogs.com/gyadmin/p/10448825.html

selenium鼠标操作 包含右击和浮层菜单的选择相关推荐

  1. selenium——鼠标操作ActionChains:点击、滑动、拖动

    from selenium.webdriver import ActionChains 1.鼠标点击 click:鼠标左击 double_click:鼠标双击 context_click:鼠标右击 b ...

  2. 爬虫Spider 08 - chromedriver设置无界面模式 | selenium - 键盘操作 | 鼠标操作 | 切换页面 | iframe子框架 | scrapy框架

    文章目录 Spider 07回顾 cookie模拟登陆 三个池子 selenium+phantomjs/chrome/firefox Spider 08 笔记 chromedriver设置无界面模式 ...

  3. python Selenium 常见操作 元素定位

    一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locating-elements.html         这里有各种策略用于定位网页中的 ...

  4. [Python从零到壹] 九.网络爬虫之Selenium基础技术万字详解(定位元素、常用方法、键盘鼠标操作)

    欢迎大家来到"Python从零到壹",在这里我将分享约200篇Python系列文章,带大家一起去学习和玩耍,看看Python这个有趣的世界.所有文章都将结合案例.代码和作者的经验讲 ...

  5. selenium对浏览器操作、鼠标操作等总结

    1 控制浏览器 Selenium 主要提供的是操作页面上各种元素的方法,但它也提供了操作浏览器本身的方法,比如浏览器的大小以及浏览器后退.前进按钮等. 1.1 控制浏览器窗口大小 在不同的浏览器大小下 ...

  6. java鼠标右击出现选择窗口_java菜单代码 java中鼠标右击弹出菜单怎样实现

    帮忙给一个java菜单栏例子的源代码 给你个小例子,已经添加注释了.自己运行下看看效果,满意的话记得结贴子. import java.awt.BorderLayout; import java.awt ...

  7. selenium之鼠标操作详解

    前言 人类频繁的用手操作鼠标和键盘,为了解决这个问题,selenium工具为我们提供了一个类来处理这些事件- Actionchains ,该类可以完成鼠标移动,鼠标点击事件.键盘输入.内容菜单交互等交 ...

  8. selenium自动化之鼠标操作

    在做自动化测试的时候,经常会遇到这种情况,某个页面元素,你必须要把鼠标移动到上面才能显示出元素.那么这种情况,我们怎么处理呢?,selenium给我们提供了一个类来处理这类事件--ActionChai ...

  9. pythonselenium教程模拟鼠标和键盘_【02篇】python+selenium实现Web自动化:鼠标操作和键盘操作!...

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第二篇博 ...

最新文章

  1. Vue项目中获取数据后使用swiper轮播,无法轮播且 autoplay 和 loop 失效问题!
  2. Tips--图像特征提取方法HOG、SIFT、LBP优缺点对比
  3. 解决“Pip - Fatal error in launcher: Unable to create process using... ”
  4. codevs 3160 最长公共子串
  5. 国家电网面试题计算机类,国家电网招聘面试题及参考答案
  6. vscode有趣插件
  7. 计算机系统如何禁止删除文件,如何彻底删除文件防止恢复【详细介绍】
  8. ActiveMQ学习笔记(4)----JMS的API结构和开发步骤
  9. 爆肝六万字整理的python基础,快速入门python的首选
  10. 马斯克群发卫星造天文奇观,未来三天全国多地可见
  11. 【读书笔记】《解忧程序员》读后感
  12. 从控制台输入两个英文字母,输出这两个英文字母之间的所有的字母(包含大小写)
  13. 347.前K个高频元素 C++
  14. SpringMVC框架 |自定义类型转换器与日期格式化
  15. javaftp读取服务器文件,java读取ftp服务器文件
  16. 《爱上跑步的13周》,让你拥有健康美丽的人生
  17. 存储技术现在的困境以及未来的发展
  18. 斐讯N1刷无线打印服务器,N1刷op固件的小白步骤 - 斐讯无线路由器以及其它斐迅网络设备 - 恩山无线论坛 - Powered by Discuz!...
  19. 关于SVN安装目录下,没有svn.exe程序的解决
  20. latex数学符号(持续更新)

热门文章

  1. stm32for循环几个机械周期_波浪理论之五:循环周期理论
  2. python下载器2
  3. oracle查询 :一个角色包括的系统权限,对象权限,Oracle有多少种角色,某个用户有什么角色
  4. 分布式服务器客户端实验
  5. java导出excel并压缩
  6. 进制转换问题---例如把26进制转为10进制
  7. batch-size 深度学习笔记
  8. spark 调度模块详解及源码分析
  9. 在线学习在爱奇艺信息流推荐业务中的探索与实践
  10. Java Spark之创建RDD的两种方式和操作RDD