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 ...
随机推荐
- msyql 主从配置
vim /etc/mysql/my.cnf; # 以下部分一定要配置在[mysqld]后面 [mysqld] log-bin=mysql-bin server-id= //设置数据库服务器唯一ID,这 ...
- ac自动机板子
hdu2222 #include<bits/stdc++.h> #define ll long long #define M 500005 using namespace std; int ...
- java Concurrent包学习笔记(六):Exchanger
一.概述 Exchanger 是一个用于线程间协作的工具类,Exchanger用于进行线程间的数据交换,它提供一个同步点,在这个同步点,两个线程可以交换彼此的数据.这两个线程通过exchange 方法 ...
- linux命令tee用法
功能说明:读取标准输入的数据,并将其内容输出成文件. 语 法:tee [-ai][--help][--version][文件…] 补充说明:tee指令会从标准输入设备读取数据,将其内容输出到标准输出设 ...
- denyhost部署
部署 tar xzf DenyHosts-2.6.tar.gz -C /uc cd /uc/DenyHosts-2.6 python setup.py install running install ...
- c#中数组array和list在函数间传递 转置
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- mysql学习之路_联合查询与子查询
联合查询 联合查询:将多次查询(多条select语句)在记录上进行拼接(字段不会增加). 语法:多条select语句构成,每条select语句获取的字段必须严格一致(但是字段类型无关). Select ...
- HDMI ip中的时钟 vid_clk与ls_clk
由TMDS_Bit_clock_Ratio.TMDS_clk和色彩深度,就可以确定出tmds_clk,cdr_clk,vid_clk和ls_clk之间的关系. 1.Tmds_clk时钟频率的确定: 原 ...
- ==和equals的比较
一 : == 的特点: a == b ; 1.如果A和B是基本数据类型 == 比较的是两个变量的值 2.如果A和B是引用数据类型 == 比较的是两个变量的内存地址 二:重写的equal ...
- java web 大文件下载
泽优大文件下载产品测试 泽优大文件下载控件down2,基于php开发环境测试. 开发环境:HBuilder 服务器:wamp64 数据库:mysql 可视化数据库编辑工具:Navicat Premiu ...