第4章 Selenium2-java WebDriver API (三)
4.12 上传文件
4.12.1 sendKeys实现上传
html
<html>
<head>
</head>
<body>
<div class="row_fluid">
<div class="span10 well">
<h3>Upfile</h3>
<input type="file" name="file"/>
</div>
</div>
</body>
</html>
java代码:
package upfile; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class Upfile {
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
WebDriver driver = new FirefoxDriver();
File file = new File("C:/Users/Administrator/Desktop/upfile.html");
String filePath = file.getAbsolutePath();
driver.get(filePath); driver.findElement(By.name("file")).sendKeys("D:\\BugReport.txt");
Thread.sleep(2000);
driver.close();
} }
4.14 下载文件
package upfile; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile; public class Download {
public static void main(String[] args) {
FirefoxProfile firefox=new FirefoxProfile();
//browser.download.folderList 设置成0代表下载到浏览器默认下载路径,设置成2则可以保存到指定目录。
firefox.setPreference("browser.download.folderList", 2);
// browser.download.manager.showWhenStarting 是否显示开始;Ture为显示,Flase为不显示。
firefox.setPreference("browser.download.manager.showWhenStarting", false);
//browser.download.dir 用于指定所下载文件的目录。os.getcwd() 函数不需要传递参数,用于返回当前的目录。
firefox.setPreference("browser.download.dir", "d:\\Program Files");
// browser.helperApps.neverAsk.saveToDisk 指定要下载页面的Content-type值,“application/octet-stream”为文件的类型。
// HTTP Content-type常用对照表:http://tool.oschina.net/commons
firefox.setPreference("browser.helpApps.neverAsk.saveToDisk", "application/octet-stream"); WebDriver driver=new FirefoxDriver(firefox);
driver.get("http://pan.baidu.com/share/link?shareid=3048009203&uk=375774229#list/path=%2F");
driver.findElement(By.xpath(".//*[@id='shareqr']/div[2]/div[2]/div/ul[1]/li[1]/div/span[1]")).click();
driver.findElement(By.xpath(".//*[@id='bd-main']/div/div[1]/div/div[2]/div/div/div[2]/a[2]/span/span")).click();
driver.findElement(By.xpath(".//*[@id='_disk_id_3']/span")).click(); }
}
4.15 操作Cookie
package com.cy.selenium;
import java.util.Set; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class Cookie { public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://www.baidu.com/");
/*WebDriver操作cookie的方法::
·getCookies() 获得所有cookie信息。
·getCookieNamed(String name) 返回字典的key为“name”的cookie信息。
·addCookie(cookie dict) 添加cookie。“cookie_dict”指字典对象,必须有name 和value 值。
·deleteCookieNamed(String name) 删除cookie信息。“name”是要删除的cookie的名称;“optionsString”是该cookie的选项,目前支持的选项包括“路径”,“域”。
·deleteAllCookies() 删除所有cookie信息。
*/
Set<org.openqa.selenium.Cookie> coo=driver.manage().getCookies();
System.out.println(coo);
} }
4.16 调用JavaScript
package com.cy.selenium; import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor; public class JavaScript {
public static void main(String[] args) throws InterruptedException {
System.out.println("===============");
WebDriver driver=new FirefoxDriver();
driver.manage().window().setSize(new Dimension(700, 600));
driver.get("http://www.baidu.com/");
driver.findElement(By.id("kw")).sendKeys("JavaScript");
driver.findElement(By.id("su")).click();
Thread.sleep(2000);
// 拖动滚动条 window.scrollTo(左边距,上边距);
((JavascriptExecutor)driver).executeScript("window.scrollTo(100,450);"); Thread.sleep(3000);
System.out.println("end"); driver.quit();
} }
第4章 Selenium2-java WebDriver API (三)的更多相关文章
- python+selenium自动化软件测试(第2章):WebDriver API
2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...
- selenium2.0(WebDriver) API
1.1 下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guide: h ...
- 《Java从入门到失业》第二章:Java环境(三):Java命令行工具
2.3Java命令行工具 2.3.1编译运行 到了这里,是不是开始膨胀了,想写一段代码来秀一下?好吧,满足你!国际惯例,我们写一段HelloWorld.我们在某个目录下记事本,编写一段代码如下: 保存 ...
- selenium2(WebDriver) API
selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/ 1.1 下载selenium2.0的包 官方downl ...
- java JDK8 学习笔记——第11章 线程和并行API
第11章 线程与并行API 11.1 线程 11.1.1 线程 在java中,如果想在main()以外独立设计流程,可以撰写类操作java.lang.Runnable接口,流程的进入点是操作在run( ...
- [selenium webdriver Java]常用api
1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 public class GetText { @ ...
- Selenium2+Python:Webdriver API速记手册
由于web自动化常常需要控制浏览器行为和操作页面元素,相关函数又比较多,于是再此记下一份Webdriver API查阅文档以备不时之需. 参考:虫师<Selenium2自动化测试实战>,和 ...
- java基础(三章)
java基础(三章) 一.基本if结构 1.流程图 l 输入输出 l 判断和分支 l 流程线 1.1 简单的if条件判断 if(表达式){ //表 ...
- java中的数据类型,运算符,字符串,输入输出,控制流,大数值,数组; 《java核心技术卷i》 第三章:java基本程序结构;
<java核心技术卷i> 第三章:java基本程序结构: 每次看书,去总结的时候,总会发现一些新的东西,这次对于java的数组有了更深的了解: java中的数据类型,运算符,字符串,输入输 ...
随机推荐
- Luogu3587[POI2015]POD - hash + 单调队列
Solution 还是去看了题解. 感谢大佬的博客→ 题解传送门 是一道思路比较新的题. 搞一个前缀和, 记录前 $i$ 个位置每种颜色的出现次数, 如果位置 $i$ 是 颜色 $a[i]$ 的最后 ...
- 多线程安全单例模式学习代码 c++11
// Singleton.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <mutex> #include & ...
- 【轻松前端之旅】CSS选择器中的空格与尖括号有何区别?
CSS选择器中的空格与尖括号有何区别? 例子1: .a .b { margin: 0; } 空格隔开a和b,选择所有后代元素. 例子2: .a>.b { margin: 0; } 尖括号隔开a和 ...
- IT资产管理—采购与合同管理功能
- Java集合:ConcurrentHashMap原理分析
集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...
- 第39章:MongoDB-集群--Replica Sets(副本集)---副本集基本原理
①操作日志oplog Oplog是主节点的local数据库中的一个固定集合,按顺序记录了主节点的每一次写操作,MongoDB的复制功能是使用oplog来实现的,备份节点通过查询这个集合就可以知道需要进 ...
- Java面试题4
一.JAVA基础篇-概念1.简述你所知道的Linux: Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机(A ...
- form表单保存和取出
function saveConfig() { var configName = document.title; if (!localStorage) return; var Config = {}; ...
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十四之铭文升级版
铭文一级: 第11章 Spark Streaming整合Flume&Kafka打造通用流处理基础 streaming.conf agent1.sources=avro-sourceagent1 ...
- linux下设置计划任务执行python脚本
linux下设置计划任务执行python脚本 简介 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自 ...