第4章 Selenium2-java WebDriver API (三)

來源:互聯網
上載者:User

標籤:quit   help   ati   err   path   get   let   span   static   

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 (三)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.