selenium測試(Java)--元素操作(五)


元素的操作有

1. 清除文本

2. 模擬按鍵輸入

3. 單擊元素

4. 返回元素尺寸

5. 獲取文本

6. 獲取屬性值

7. 判斷是否可見

8. 提交

 

 

下面通過操作新浪郵箱的注冊界面的腳本來展示使用方法

 

源代碼:

 1 package com.test.elementoperation;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.WebElement;
 6 import org.openqa.selenium.firefox.FirefoxDriver;
 7 
 8 public class ElementOperationEmail {
 9 
10     public static void main(String[] args) {
11 
12         WebDriver driver = new FirefoxDriver();
13 
14         driver.get("https://mail.sina.com.cn/register/regmail.php");
15         driver.manage().window().maximize();
16 
17         // 獲取email名稱輸入框節點,並輸入名稱
18         WebElement emailName = driver.findElement(By.cssSelector("[name=email]"));
19  emailName.clear(); 20  emailName.click(); 21  emailName.sendKeys(AllInfo.emailName); 22 
23         // 獲取email密碼輸入框節點,在輸入密碼之前,先驗證一下email名稱時候可用,如果可用就繼續,如果不可用就退出瀏覽器
24         WebElement emailPassword = driver.findElement(By.cssSelector("[name=psw]"));
25         emailPassword.click();// 點擊一下密碼框,使得email名稱驗證信息出現
26         waitTime(3000);
27         // 獲取email名稱驗證信息節點,並判斷信息是否為"左箭頭"
28         WebElement checkName = driver
29                 .findElement(By.xpath("html/body/div[2]/div/div/div/div/form[1]/div[2]/ul/li[1]/div[3]/i"));
30         String checkContent = checkName.getText();// 通過getText方法來獲取節點文本信息
31         System.out.println("驗證用戶名信息是否存在: " + checkName.isDisplayed() + "  比對結果的信息是 :" + checkContent);
32         // 獲取到信息后開始判斷,並進行不同的分支
33         if ("左箭頭".equals(checkContent)) {
34             // 確認名稱無誤后輸入密碼
35             emailPassword.sendKeys(AllInfo.emailPassword);
36             waitTime(3000);
37 
38             // 獲取驗證碼輸入框節點,在輸入驗證碼之前,先驗證一下密碼是否有效和密碼強度
39             WebElement emailImgvcode = driver.findElement(By.cssSelector("[name=imgvcode]"));
40             emailImgvcode.click();
41             waitTime(3000);
42             // 獲取密碼校驗信息節點,並判斷時候存在以及信息是否為"密碼強度:高"
43             WebElement checkPassword = driver.findElement(By.cssSelector("[class=passWord3]"));
44             if (checkPassword.isDisplayed() && "密碼強度:高".equals(checkPassword.getText())) {
45 
46                 // 密碼校驗通過后,獲取驗證驗證圖片節點,並通過一下方法來獲取該節點的信息
47                 WebElement img = driver.findElement(By.cssSelector("[id=capcha]"));
48                 System.out.println("驗證圖片的 hight是: " + img.getSize().getHeight());
49                 System.out.println("驗證圖片的 Width是: " + img.getSize().getWidth());
50                 System.out.println("驗證圖片的 src屬性值是: " + img.getAttribute("src"));
51                 waitTime(3000);
52 
53                 // 輸入驗證碼,真實環境中selenium很難獲取到正確的驗證碼,如果在測試環境可以通過訪問Cookie的方式實現。
54                 // 這里隨意輸入一個驗證碼
55                 emailImgvcode.sendKeys("1234567890");
56                 waitTime(3000);
57 
58                 // 獲取提交按鈕信息,並通過一些方法來獲取該節點的信息
59                 WebElement submit = driver.findElement(By.cssSelector("[class=subIco]"));
60                 System.out.println("提交按鈕的文本信息是: " + submit.getText());
61                 System.out.println("提交按鈕的class屬性值是: " + submit.getAttribute("class"));
62                 System.out.println("提交按鈕的style屬性值是: " + submit.getAttribute("style"));
63                 System.out.println("提交按鈕的css屬性值是: " + submit.getCssValue("float"));
64                 System.out.println("提交按鈕的href屬性值是: " + submit.getAttribute("href"));
65                 submit.submit();
66                 waitTime(5000);
67 
68                 driver.quit();
69 
70             } else {
71                 System.out.println("密碼校驗信息沒有展示或者密碼強度低");
72                 driver.quit();
73             }
74 
75         } else {
76             System.out.println("用戶名不可用");
77             driver.quit();
78         }
79 
80     }
81 
82     static public void waitTime(int time) {
83 
84         try {
85             Thread.sleep(time);
86         } catch (InterruptedException e) {
87             // TODO Auto-generated catch block
88             e.printStackTrace();
89         }
90     }
91 }

 

執行結果

1 驗證用戶名信息是否存在: true  比對結果的信息是 :左箭頭
2 驗證圖片的 hight是: 34
3 驗證圖片的 Width是: 118
4 驗證圖片的 src屬性值是: https://mail.sina.com.cn/cgi-bin/createcode.php?t=1468141676
5 提交按鈕的文本信息是: 立即注冊
6 提交按鈕的class屬性值是: subIco
7 提交按鈕的style屬性值是: float: left;
8 提交按鈕的css屬性值是: left
9 提交按鈕的href屬性值是: javascript:void(0)

 

下面是頁面與利用firebug查看到的信息截圖

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM