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)的更多相关文章

  1. WebDriver高级应用实例(10)

    10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...

  2. WebDriver高级应用实例(9)

    9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...

  3. WebDriver高级应用实例(8)

    8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...

  4. WebDriver高级应用实例(7)

    7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...

  5. WebDriver高级应用实例(6)

    6.1精确比较网页截图图片 目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较.确认页面是否发生了改变 被测网页的网址: http://www.baidu.com Ja ...

  6. WebDriver高级应用实例(5)

    5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...

  7. WebDriver高级应用实例(4)

    4.1操作web页面的滚动条 被测网页的网址: http://v.sogou.com Java语言版本的API实例代码 import org.testng.annotations.Test; impo ...

  8. WebDriver高级应用实例(2)

    2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...

  9. WebDriver高级应用实例(1)

    1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...

随机推荐

  1. Android APP测试流程

    一. Monkey测试(冒烟测试) 使用monkey测试工具进行如下操作: 1. APP的安装 2. APP随机操作测试(APP压力测试) 3. APP的卸载 二. 安装卸载测试 1. 使用测试真机进 ...

  2. activemq部署

    系统环境 IP salt-master-1:192.168.0.156 salt-master-2:192.168.0.157 node-test-1:192.168.0.158 node-test- ...

  3. mysql学习之路_联合查询与子查询

    联合查询 联合查询:将多次查询(多条select语句)在记录上进行拼接(字段不会增加). 语法:多条select语句构成,每条select语句获取的字段必须严格一致(但是字段类型无关). Select ...

  4. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

  5. hadoop hive组件介绍及常用cli命令

    Hive架构图 Hive产生原因 1 关系型数据库以产生多年sql成熟 2 简化开发降低成本 3 java成员可编写udf函数 Hive是什么 Hive是基于hadoop的一个数据库工具,使用Hql作 ...

  6. 词袋模型(BOW, bag of words)

    词集模型:单词构成的集合,每个单词只出现一次. 词袋模型:把每一个单词都进行统计,同时计算每个单词出现的次数. 在train_x中,总共有6篇文档,每一行代表一个样本即一篇文档.我们的目标是将trai ...

  7. Codeforces Round#415 Div.2

    A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...

  8. 20155205《Java程序设计》实验五(网络编程与安全)实验报告

    20155205 <Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 (一) 两人一组结对编程 参考http://www.cnblogs.com/rocedu/p/6 ...

  9. 20145232 韩文浩 《Java程序设计》第3周学习总结

    教材学习内容总结 在第三章中,知道了Java可区分为基本类型和类类型两大类型系统,其中类类型也称为参考类型.在这一周主要学习了类类型. 对象(Object):存在的具体实体,具有明确的状态和行为 类( ...

  10. Python3 安装 PyQt5 -pycharm 环境搭建

    执行命令: pip3 install PyQt5 PyQt5+python3+pycharm开发环境配置   1.下载PyQt 官方网站:http://www.riverbankcomputing.c ...