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 ...
随机推荐
- Codeforces gym 102062 简要题解
文章目录 A. Bob and BoB B. Vibranium Gift C. The Blood Moon D. Palindrome and Chocolate E. Jumpy Robot F ...
- 2019.01.14 codeforces685B. Kay and Snowflake(树形dp)
传送门 题意简述:给出一棵树,求每个子树的重心. 首先通过画图可以观察出一个性质,我们从叶子结点向根节点递推重心的话重心的位置是不会下降的. 然后由于一个点的重心要么是自己,要么在重儿子子树内,因此如 ...
- 2018.12.14 codeforces 922E. Birds(分组背包)
传送门 蒟蒻净做些水题还请大佬见谅 没错这又是个一眼的分组背包. 题意简述:有n棵树,每只树上有aia_iai只鸟,第iii棵树买一只鸟要花cic_ici的钱,每买一只鸟可以奖励bbb块钱,从一棵 ...
- 2018.11.24 poj3693Maximum repetition substring(后缀数组)
传送门 后缀数组好题. 考虑枚举循环节长度lenlenlen. 然后考虑枚举循环节的起点来更新答案. 但是直接枚举每次O(n)O(n)O(n). 考虑枚举len∗k+1len*k+1len∗k+1作为 ...
- java json注解
(1)初级我们从几个简单的使用场景开始:重命名属性,忽略属性,以及修改属性所使用的类型.注意:下面的例子仅仅显示了成员属性(field properties),注解同样也可以用在成员方法(getter ...
- maven使用中遇到的问题
一>手动将jar包安装到仓库的命令示例: 首先:编写命令:mvn install:install-file -Dfile=D:\lucene-highlighter-4.10.2.jar -Dg ...
- urb的处理流程
USB分析 2013年11月11日 16:06:24 阅读数:1807 转载自:http://blog.csdn.net/aaa6695798/article/details/4776202 很久 ...
- python 基础1
一.python版本的介绍 python有两个大的版本2.X与3.X的版本,而在不久的将来将全面的进入3的版本.3的版本将比2的版本功能更加强大,而且也修复了大量的bug. 二.python的安装可以 ...
- SDIBT 2345 (3.2.1 Factorials 阶乘)
Description N的阶乘写作N!表示小于等于N的所有正整数的乘积.阶乘会很快的变大,如13!就必须用32位整数类型来存储,70!即使用浮点数也存不下了.你的任务是找到阶乘最后面的非零位.举个例 ...
- ArcGIS API for Silverlight部署本地地图服务
这一节我们来讲新建立的ArcGIS API for Silverlight应用程序如何加载自己的地图服务的问题,网上的资料讲的都有点含糊不清,这次我们详细的讲一下配置的步骤: 首先介绍下我们的开发和部 ...