感谢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 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 1
; Wait 10 seconds for the Upload window to appear WinWait("[CLASS:#32770]","",10) ; Set input focus to the edit control of Upload window using the handle returned by WinWait ControlFocus("另存为","","Edit1") Sleep(2000) ; Set the File name text on the Edit field ControlSetText("另存为", "", "Edit1", "d:\autoit-v3-setup") Sleep(2000) ; 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鼠标操作 包含右击和浮层菜单的选择的更多相关文章

  1. selenium鼠标操作

    #-*- coding:utf-8 -*- import time from selenium import webdriver from selenium.webdriver.common.acti ...

  2. Python+Selenium - 鼠标操作

    鼠标操作类:action_chains模块的ActionChains类 使用组成:操作 + 执行(perform()) 导入代码 from selenium.webdriver.common.acti ...

  3. Java编程语言下Selenium 鼠标悬停以及右击操作

    // 基于Actions类创建一个对象 Actions action = new Actions(driver); // 鼠标悬停在药渡公司全称字段上 action.moveToElement(Yao ...

  4. Selenium常用API的使用java语言之8-模拟鼠标操作

    通过前面例子了解到,可以使用click()来模拟鼠标的单击操作,现在的Web产品中提供了更丰富的鼠标交互方式, 例如鼠标右击.双击.悬停.甚至是鼠标拖动等功能.在WebDriver中,将这些关于鼠标操 ...

  5. selenium自动化之鼠标操作

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

  6. selenium - webdriver - ActionChains类(鼠标操作)

    ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_click() ...

  7. selenium + python(鼠标操作)

    关于最近学习selenium自动化测试鼠标操作的一些总结 常见的鼠标操作

  8. python+selenium 鼠标事件操作

    一.前言 除了可以使用 click( ) 来模拟鼠标的单击操作,现在Web产品中还提供了更丰富的鼠标交互方式,例如鼠标右键.双击.悬停.拖动等功能,在WebDriver中,将这些关于鼠标操作的方法都封 ...

  9. Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载

    元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...

随机推荐

  1. Angular 学习笔记(二)

    控制器: 就像 JavaScript 里的构造函数一般,用来增强作用域(scope),当一个控制器通过 ng-controller 指令来添加到 DOM 中时, ng 会调用该控制器的构造函数来生成一 ...

  2. softmax求导、cross-entropy求导及label smoothing

    softmax求导 softmax层的输出为 其中,表示第L层第j个神经元的输入,表示第L层第j个神经元的输出,e表示自然常数. 现在求对的导数, 如果j=i,   1 如果ji, 2 cross-e ...

  3. 初学JavaScript正则表达式(二)

    正则表达式的实例化与标识符 字面量: var reg = /\bis\b/g // \b--字符边界 g全文搜索 查找单词为is的字符 He is a boy. IS He? 构造函数: var re ...

  4. 201271050130-滕江南《面向对象程序设计(java)》第十一周学习总结

    项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/ ...

  5. (day54)六、事务、分组、F、Q、常用字段、事务

    目录 一.聚合查询aggregate 二.分组查询annotate 三.F与Q查询 (一)F查询 1. 查询库存数大于卖出数的书籍 2. 将所有书的价格上涨100块 3.将所有书的名称后面全部加上 & ...

  6. 剑指Offer-13.调整数组顺序使奇数位于偶数前面(C++/Java)

    题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 分析: 这道题做法有很 ...

  7. JDOJ3011 铺地板III

    JDOJ3011 铺地板III https://neooj.com/oldoj/problem.php?id=3011 题目描述 有3 x N (0 <= N <= 105)的网格,需要用 ...

  8. RPC调用和HTTP调用的区别

    很长时间以来都没有怎么好好搞清楚RPC(即Remote Procedure Call,远程过程调用)和HTTP调用的区别,不都是写一个服务然后在客户端调用么?这里请允许我迷之一笑~Naive!本文简单 ...

  9. 怎样用cmd脚本添加Qt的环境变量

    在网上遍历了很久,终于找到了一个简单且令人满意的答案: 定位到PyQt5发布文件所需的plugins的位置: 新建一个名为“设置环境变量”的cmd脚本,在里面写上: wmic ENVIRONMENT ...

  10. Paper | Toward Convolutional Blind Denoising of Real Photographs

    目录 故事背景 建模现实噪声 CBDNet 非对称损失 数据库 实验 发表在2019 CVPR. 摘要 While deep convolutional neural networks (CNNs) ...