首页 技术 正文
技术 2022年11月10日
0 收藏 626 点赞 3,526 浏览 5927 个字

感谢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鼠标操作 包含右击和浮层菜单的选择

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

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

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

也有在网上看到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,拿到的只能是浏览器的信息

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

废话不多说,test case 如下

1.打开autoIt的官网

2.click download 页面

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

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

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

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

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

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

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

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

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

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

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

autoIt使用方法:

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

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

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

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

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

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

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

使用java的runTime类调用

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

全部代码如下:

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

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鼠标操作 包含右击和浮层菜单的选择

效果图:

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295