WebDriver高级应用实例(3)
3.1自动化下载某个文件
被测网页的网址:
https://pypi.org/project/selenium/#files
Java语言版本的API实例代码
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; public class download {
WebDriver driver;
String url="https://pypi.org/project/selenium/#files";
@Test
public void testOne() throws Exception {
//使用Chrome浏览器自动下载文件并保存到指定的文件路径
//或 使用Selenium更改Chrome默认下载存储路径
DesiredCapabilities caps = setDownloadsPath();//更改默认下载路径
driver = new ChromeDriver(caps);
driver.manage().window().maximize();
driver.get(url);
WebElement myElement = driver.findElement(By.xpath("//a[contains(text(),'selenium-3.141.0.tar.gz')]"));
Actions action = new Actions(driver);
myElement.click();//点击下载
Thread.sleep(10000);
} //单独重构成一个方法,然后调用
public DesiredCapabilities setDownloadsPath() {
String downloadsPath = "E:\\downloadFiles";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadsPath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(ChromeOptions.CAPABILITY, options);
return caps;
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize(); } @AfterMethod
public void afterMethod() {
driver.quit();
}
}
3.2使用sendKeys方法上传一个文件附件
被测网页的网址:
<html>
<body>
<from enctype="multipart/form-data" action="parse_file" method="post">
<p>Browse for a file to upload:</P>
<input id="file" name="file" type="file"></input>
<br/><br/>
<input type="submit" id="filesubmit" value="SUMBMIT"></input>
</from>
</body>
</html>
Java语言版本的API实例代码
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod; public class upLoad {
WebDriver driver;
String url ="file:///E:/%E6%9D%90%E6%96%99/selenium/upload.html";
@Test
public void testUploadFile() {
driver.get(url);
//找到文件上传输入框
WebElement fileInputBox = driver.findElement(By.id("file"));
//输入文件路径
fileInputBox.sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg");
//设置显示等待5秒
WebDriverWait wait = new WebDriverWait(driver, 5);
//显示等待判断页面按钮是否处于可单击状态
wait.until(ExpectedConditions.elementToBeClickable(By.id("filesubmit")));
//找到提交按钮
WebElement fileSubMitButton = driver.findElement(By.id("filesubmit"));
//单击提交按钮
fileSubMitButton.click(); }
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
代码说明:仅做测试,上传功能大家套用至自己需要测试的页面即可
WebDriver高级应用实例(3)的更多相关文章
- WebDriver高级应用实例(10)
10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...
- WebDriver高级应用实例(9)
9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...
- WebDriver高级应用实例(8)
8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...
- WebDriver高级应用实例(7)
7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...
- WebDriver高级应用实例(6)
6.1精确比较网页截图图片 目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较.确认页面是否发生了改变 被测网页的网址: http://www.baidu.com Ja ...
- WebDriver高级应用实例(5)
5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...
- WebDriver高级应用实例(4)
4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...
- WebDriver高级应用实例(2)
2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...
- WebDriver高级应用实例(1)
1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...
随机推荐
- Win7 VS2013环境使用cuda_7.5.18
首先得吐槽下VS2015出来快一年了CUDA居然还不支持,没办法重装系统刚从2013升到2015,还得再装回一个2013用,只为学习CUDA... 然后安装的时候,如果你选择自定义组件安装,注意不要改 ...
- mysql order by 中文 排序
mysql order by 中文 排序 1. 在MySQL中,我们经常会对一个字段进行排序查询,但进行中文排序和查找的时候,对汉字的排序和查找结果往往都是错误的. 这种情况在MySQL的很多版本中都 ...
- IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (二)
第二部分 邮件发送历史 一.第一封邮件 1.1969年10月,世界上的第一封电子邮件 1969年10月世界上的第一封电子邮件是由计算机科学家Leonard K.教授发给他的同事的一条简短 ...
- MySql Cast与Convert函数
两者具体的语法如下: Cast(value as type): Convert(value ,type): type不是都可以滴,可以转换的type如下: 二进制,同带binary前缀的效果 : BI ...
- mysql学习之路_sql
查看数据库: Show databases; 查看指定部分数据库:模糊查询 Show databases like ‘patten’;--paatten是匹配模式 %:表示是匹配模式 _:表示匹配单个 ...
- .net升级到4.0之后,出现;System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798
今天在做从Silverlight页中跳转到aspx页的时候,出现错误: 第一次跳转的时候就出现这个错误,然后在点跳转或者刷新这个错误页面,问题就没有了. 解决方案: 在C:\Program Files ...
- codehouse
1 # 整数部分十进制转二进制 2 3 num = int(raw_input(">>>")) 4 5 if num < 0: 6 isNeg = True ...
- 使用Servlet动态生成验证码
最近在重新看了一遍servlert,看到篇优质博客推荐给大家:https://www.cnblogs.com/xdp-gacl/p/3798190.html 顺便把学习过程中的知识记录下来. 今天是如 ...
- java基础-day9
第09天 java集合 今日内容介绍 u 对象数组 u 集合类之ArrayList u 学生管理系统案例 第1章 对象数组 1.1 对象数组概述 A:基本类型的数组:存储的元素为基本类型 ...
- Mahout简介
Mahout简介 一.mahout是什么 Apache Mahout是ApacheSoftware Foundation (ASF)旗下的一个开源项目,提供了一些经典的机器学习的算法,皆在帮助开发人员 ...