上传文件

 

元素标签是input时上传方式

上传是一个input,对于这种元素是input

的标签可以采用元素的sendKeys()方法来直接赋值,upload.html代码:

<html>
<head></head>
<body>
<input type="file" id="fileControl" value="选择文件">
</body>
<style type="text/css" id="1"></style>
</html>

 

具体代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Upload1 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
//打开上传的网页 - get中输入upload的地址
driver.get("D:\\Work\\eclipse_workspace\\seleniumdemo\\res\\upload.html");
WebElement e1 = driver.findElement(By.id("fileControl"));
//输入要上传文件的地址
e1.sendKeys("D:\\Work\\eclipse_workspace\\seleniumdemo\\res\\被上传的文件.txt");
try {
//等待2秒观察显示
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.quit();
}
}

 

使用autoit脚本上传方式

它是一个使用类似BASIC脚本语言的免费软件,它设计用于Windows GUI(图形用户界面)中进行自动化操作。它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务。

autoit安装包地址链接: http://pan.baidu.com/s/1dDL5Qhz 密码: yew5

二进制文件安装即可,打开autoit,主界面如下:

具体使用:

拖动Finder Tool到要定位到的弹窗上

获取到文件名输入框的classname

获取到打开按钮的classname

根据获取到的值编写autoit的脚本:

;fisrt make sure the number of arguments passed into the script is more than 1
If $CmdLine[0]<2 Then Exit ElseIf; if parmas num<2,then Break
;CmdLine[0];参数的数量
;CmdLine[1];第一个参数(脚本名称后面)
;CmdLine[2];第二个参数
;都是从cmd传入参数
handleUpload($CmdLine[1],$CmdLine[2])
;定义上传函数,有两个参数,第一个是浏览器的名字,第二个参数是文件路径
Func handleUpload($browser, $uploadfile)
Dim $title;定义一个title变量
;根据弹窗的title来判断是什么浏览器
If $browser = "ie" Then;代表chrome浏览器
$title="选择要加载的文件"
ElseIf $browser = "chrome" Then;
$title="打开"
ElseIf $browser="firefox" Then
$title="文件上传"
EndIf If WinWait($title,"",4) Then;等待弹窗出现,最大等待时间是4s
WinActivate($title);找到弹窗之后,激活当前窗口
ControlSetText($title,"","Edit",$uploadfie);把文件路径放入输入框
ControlClick($title,"","Button");点击保存或者打开或者上传按钮
Else
Return False
EndIf
EndFunc

文件保存为upload.au3,将文件转换成exe可执行文件

安装目录下的C:\Program Files (x86)\AutoIt3\Aut2Exe\Aut2exe_x64.exe

生成文件upload.exe

打开cmd,输入:"C:\Users\Administrator\Desktop\upload.exe" "firefox" "C:\Users\Administrator\Desktop\image.jpg"  双引号不要省略

在selenium中调用脚本,封装成一个执行这个exe文件的方法:

/**
*上传文件,需要点击弹出上传的窗口
*
*@param browser
*使用的浏览器名称
*@param file
*需要上传的文件及文件名
*/
public void handleupload(String browser, File file) {
String filepath = file.getAbsolutePath();
String executeFile = "C:\Users\Administrator\Desktop\upload.exe";//定义了upload.exe文件的路径
String cmd = "\"" + executeFile + "\"" + " " + "\"" + browsser + "\"" + " " + "\"" + filePath + "\"";
try {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
} catch(Exception e) {
e.printStackTrace();
}
}

具体步骤是:先去用selenium点击上传文件按钮,之后会弹出上传文件输入框,最后执行上面那个public void handleUpload(String browser, File file)方法即可。

Selenium2(java)selenium常用API 五的更多相关文章

  1. java selenium常用API(WebElement、iFrame、select、alert、浏览器窗口、事件、js) 一

     WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.clic ...

  2. java selenium常用API汇总

    (WebElement.iFrame.select.alert.浏览器窗口.事件.js)     一 WebElement相关方法 1.点击操作 WebElement button = driver. ...

  3. Selenium Web 自动化 - Selenium常用API

    Selenium Web 自动化 - Selenium常用API 2016-08-01 目录 1 对浏览器操作  1.1 用webdriver打开一个浏览器  1.2 最大化浏览器&关闭浏览器 ...

  4. 详解介绍Selenium常用API的使用--Java语言(完整版)

    参考:http://www.testclass.net/selenium_java/ 一共分为二十个部分:环境安装之Java.环境安装之IntelliJ IDEA.环境安装之selenium.sele ...

  5. Selenium常用API的使用java语言之5-selenium元素定位

    1.selenium定位方法 Selenium提供了8种定位方式. id name class name tag name link text partial link text xpath css ...

  6. Java 之常用API(一)

    常用API  1 API概述  2 Scanner类与String类  3 StringBuilder类 NO.one API概述 1.1 API概述 API(Application Programm ...

  7. Java之常用API

    API概述 什么是API API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK 中提供的各种功能的 Java类,这些 ...

  8. Selenium2(java)selenium常用API 四

    WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.click ...

  9. Selenium2(java)selenium常用API 六

    selenium处理HTML5 处理Vedio WebDriver driver = new FirefoxDriver(); driver.get("http://videojs.com/ ...

随机推荐

  1. java网络之udp

    import java.net.*; /** * InetAddress用法 * 获取本地主机名,ip * 通过主机名,ip获取InetAddress对象 */ public class Demo{ ...

  2. js动态添加file控件

    <html></head><script language="javascript" type="text/ecmascript" ...

  3. Windows环境下使用VS2005编译OpenSSL

    如何Windows环境下,使用VS2005编译OpenSSL,虽然这个问题在Baidu.Google上一堆,但安装中还是遇到些问题,在这里 记录下来希望能帮助大家不要在走弯路.注:我是在WinXP S ...

  4. 前端复制功能的若干 -- document.execCommand()

    最近涨停科技公司实习,由于backend基础太弱...强行前端了一把..搞了两周才搞下页面里copy的功能,期间有些琐碎,恐忘,记录在此. 目前copy主流有四种方式:ZeroClipboard,Cl ...

  5. protobuf python api

    摘要: python中一切都可以看作类.那么如何查看每个类的API.使用ipython python  protobuf 的函数在message中定义 此处所有的api说明:https://devel ...

  6. 关于基本视频播放的Demo

    最近在做一个视频的Demo,当然是仿的别人的,现贴出原文地址:http://code4app.com/forum.php?mod=viewthread&tid=8959&highlig ...

  7. strlen sizeof strcat strcpy区别

      strlen(p): 能计算出p指向字符串的长度(以当前p的位置开始),不包含终止字符'\0': p可以声明为char* p或者char p[],这两种形式strlen均能正确计算. sizeof ...

  8. ice grid 完整部署过程

    待补充 一 理论准备 一个IceGrid集群有一个registry(注册表,用于定位)和多个node组成. IceGrid配置包括集群配置和应用配置: config.grid是集群配置,配置Regis ...

  9. python_json常用的方法

    1. 什么是JSON? JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 Web 客户机传递给服务器端 ...

  10. mysql 隔离级别与锁

    1.什么是事务 事务是一条或多条数据库操作语句的组合,具备ACID,4个特点. 原子性:要不全部成功,要不全部撤销 隔离性:事务之间相互独立,互不干扰 一致性:数据库正确地改变状态后,数据库的一致性约 ...