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

首先感谢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 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();

    }

}

效果图:

时间: 2024-08-10 06:29:54

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

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

1.要使用右键另存,需要先按照第三方工具AutoIt: 链接: https://pan.baidu.com/s/12aBBhOOTmyQpH9hukt0XGA 密码: fcdk 2.创建一个名为loadFile.au3的AutoItScript编辑器,内容如下: 新建一个名为loadFile.au3的AutoItScript编辑器,文件具体内容如下: ;ControlFocus("title","text",controlID) ;表示将焦点切换到标题为title窗

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

1.需要借住autoit工具和Robot类,下载地址:https://www.autoitscript.com/site/autoit/downloads/ 2.autoit的使用不再详细讲解.如下图: 3.识别到保存窗口后,用autoit编辑器编写脚本 ControlFocus("保存图片", "","Edit1") ;ControlFocus("title","text",controlID) Edit

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

1.参考 autopy (实践见最后一章节) 用Python制作游戏外挂(上) AutoPy Introduction and Tutorial autopy.mouse.smooth_move(1, 1) 可以实现平滑移动 autopy - API Reference pip install PyUserInput SavinaRoja/PyUserInput [python3.5][PyUserInput]模拟鼠标和键盘模拟 Python-模拟鼠标键盘动作 autoit selenium借助

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

在IE11中不能下载文件,右键另存为也无效. 发现 在IE11中点击“INTERNET选项”后,IE临时文件夹的地址没有显示,大小为0,修改只能让设置在8-8MB,注销再登录后,一切设置无效. 问题就出现在这,是临时文件存放的位置无效,点“移动文件夹”换一个位置. IE没有临时存放文件的文件当然就不能下载啦! 这时需要手动设一个位置,如在C盘下 新建一个文件夹IEtemp.点击移动文件夹.选中这个IEtemp文件夹.注销后OK.

【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)

一.selenium2.0简述 与一般的浏览器测试框架(爬虫框架)不同,Selenium2.0实际上由两个部分组成Selenium+webdriver,Selenium负责用户指令的解释(code),webdriver则负责对浏览器进行控制和页面解析.所以使用Selenium2.0时需要相应版本的webdriver和浏览器,程序运行过程中会通过webdriver启动一个真实的浏览器.由于webdriver+浏览器的组合,Selenium不存在对js.ajax解析的问题,它直接使用浏览器对网站代码

selenium测试(Java)--下载文件(十六)

下载文件需要在Firefox 的profile属性中配置一些参数,如下面的代码: package com.test.download; import java.io.File; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDri

selenium webdriver 启动火狐、谷歌、IE浏览器及插件下载地址

各个浏览器步骤差不多,先下载驱动,解压后把 exe 文件放到 Python 目录下即可: 以IE浏览器为例: 1. 先确定 selenium 版本 打开 cmd,输入命令:pip show selenium,可以查看到版本号为3.141.0 2. 下载对应的 IE 浏览器驱动 下载地址:http://selenium-release.storage.googleapis.com/index.html,根据 selenium 对应版本进行下载: 选择32位或者64位下载: 下载并解压后,将其放到

java+selenium+new——无人化自动下载文件——基于firefox浏览器

我自己的python参考:https://www.cnblogs.com/xiaobaibailongma/p/12078159.html FirefoxProfile fp = new FirefoxProfile(); fp.setPreference("browser.download.manager.showWhenStarting",False)           //设置为true时,启动下载时,显示浏览器的文件下载窗口,false时,不显示 fp.setPreferen

selenium WebDriver:使用TestNG和CSV文件进行数据驱动

1.如何解决CSV文件乱码问题 讲csv文件用UltraEdit-32软件打开,在底部状态栏有标识中将编码类型变为utf-8 2.具体结合csv数据驱动代码 csv文件 package cn.gloryroad; import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;import org.testng.annot