第4章 Selenium2-java WebDriver API (三)

简介: 4.12  上传文件  4.12.1  sendKeys实现上传  html Upfile    java代码: package upfile; import java.

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();
    }



}

 

 

相关文章
|
7天前
|
安全 Java 大数据
|
20天前
|
Java API
掌握Java 8 Stream API的艺术:详解流式编程(三)
掌握Java 8 Stream API的艺术:详解流式编程
15 2
|
4天前
|
设计模式 Java API
Java 可扩展 API 设计:打造灵活的应用架构
【4月更文挑战第27天】设计可扩展的 API 是构建灵活、易于维护的应用程序架构的关键。Java 提供了丰富的工具和技术来实现这一目标,使开发者能够构建具有高度可扩展性的应用程序。
22 4
|
23小时前
|
分布式计算 Java API
Java 8新特性之Lambda表达式与Stream API
【5月更文挑战第1天】本文将介绍Java 8中的两个重要特性:Lambda表达式和Stream API。Lambda表达式是一种新的函数式编程语法,可以简化代码并提高可读性。Stream API是一种用于处理集合的新工具,可以方便地进行数据操作和转换。通过结合Lambda表达式和Stream API,我们可以更加简洁高效地编写Java代码。
|
5天前
|
Java API 开发者
【专栏】Java 8新特性之Stream API详解
【4月更文挑战第27天】Java 8的Stream API是处理集合数据的新方式,强调简洁和声明式编程。它基于延迟执行和惰性求值,提供创建、中间操作(如filter、map)和终端操作(如forEach、collect)。示例展示了如何通过Stream排序、过滤、映射和聚合数据。
|
7天前
|
存储 SQL Java
Java8 Stream API 详解:流式编程进行数据处理
Java8 Stream API 详解:流式编程进行数据处理
|
7天前
|
前端开发 Java API
JavaSE&Java8 时间日期API + 使用心得
JavaSE&Java8 时间日期API + 使用心得
13 0
|
7天前
|
Java API
Java基础&API(3)
Java基础&API(3)
12 1
|
7天前
|
Java API 索引
Java基础&API(2) String、StringBuilder详解
Java基础&API(2) String、StringBuilder详解
|
7天前
|
Java 机器人 API
Java基础&常用API(1)
Java基础&常用API(1)