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 ...
随机推荐
- 2018.12.17 bzoj3667: Rabin-Miller算法(Pollard-rho)
传送门 Pollard−rhoPollard-rhoPollard−rho板题. 题意简述:给出几个数,让你判断是不是质数,如果不是质数就求出其最大质因子,数的大小为1e181e181e18以内. 先 ...
- 2018.11.02 洛谷P2312 解方程(数论)
传送门 直接做肯定会TLETLETLE. 于是考验乱搞能力的时候到了. 我们随便选几个质数来checkcheckcheck合法解,如果一个数无论怎么checkcheckcheck都是合法的那么就有很大 ...
- VBA编程中的 sheet1 与 sheets(1)的区别
[自己理解]sheet1是一个专有名词,不是任何对象的属性,只能单独使用,特指代码所在工作簿的那个sheet1(和顺序无关,是固定的一个表,sheets(1)则和顺序有关). 参考资料: 1.代码中一 ...
- Linux系统下修改环境变量PATH路径
方法一: PATH=$PATH:/etc/apache/bin 该方法只对当前会话有效,每次注销或者拿出系统,该设置就会无效 方法二: vi /etc/profile 在适当的位置写入:PATH=$P ...
- c# 数据表DataTable给devexpress的gridControl提供数据源
C# DataTable 详解 参考:https://www.cnblogs.com/Sandon/p/5175829.html http://blog.csdn.net/singgel/articl ...
- 深入浅出javascript(五)函数
全局函数 自定义函数 函数对象 函数的属性和方法 一.全局函数 全局函数不同于内置对象的方法(来源于网络),一共有7个,可以直接使用. escape( ).eval( ).isFinite( ).is ...
- Cmd Markdown语法参考
https://www.zybuluo.com/mdeditor markdown语法说明 Markdown中公式的写法 $$P(X=k)=C_n^kp^k(1-p)^{n-k}$$ 欢迎使用 Cmd ...
- Android多点触控手势基础
处理多点触控手势 多点触控就是同时把一根以上的手指放在屏幕上. 再继续往下以前需要补充一些名词: 触控手势:就是把一根或者几根手指放在屏幕上做各种动作,其中包括保留一根手指的前提下,拿起或者放下其余的 ...
- jenkins+maven+svn构建项目,及远程部署war包到tomcat上
要使用jenkins构建项目,当然要使用jenkins了,我使用的war版本的jenkins jenkins的官网 http://jenkins-ci.org/ 点击latest下载,但是可能因为天朝 ...
- 微软新一代Surface,该怎么看?
近日,微软在美国纽约发布了其全新一代产品——Surface 2和Surface Pro 2.如果留意微软官方商城的话,可以看到该产品现已全面开放预购.那么,这样一款产品到底怎么样?让我们来一个横向的对 ...