版權聲明:本文為博主原創文章,轉載請注明出處。
前面已經學習了定位元素,定位只是第一步,定位之后需要對這個元素進行操作,如在百度搜索首頁的輸入框進行輸入文本,對"百度一下"按鈕進行單擊等,下面就來對這些最常用的方法做一個總結。
| 方法 |
描述 |
用法示例 |
| void clear() |
清空“文本輸入元素”的值; 對其他元素沒有影響。 |
示例:清除百度搜索輸入欄輸入的文本; 方法:driver.findElement(By.id("kw")).clear(); |
| void click() |
單擊此元素。 |
示例:點擊百度搜索主頁的“新聞”文本鏈接; 方法:driver.findElement(By.linkText("新聞")).click(); |
| WebElement findElement(By by) |
使用給定的方法找到的第一個元素; |
示例:定位百度搜索主頁的搜索輸入框; 方法:driver.findElement(By.id("kw")); |
| java.util.List<WebElement> findElements(By by) |
使用給定的機制查找當前上下文中的所有元素; |
示例:查找所有的 input 元素; 方法:List<WebElement> inputs = driver.findElements(By.tagName("input")); 備注:可借助List的相關方法進行進一步定位、操作; |
| java.lang.String getAttribute(java.lang.String name) |
獲得給定的元素某一屬性的值; |
示例:獲得“百度搜索輸入框”type屬性的值; 方法:System.out.println("輸入框type屬性:"+ driver.findElement(By.id("kw")).getAttribute("type")); |
| java.lang.String getCssValue(java.lang.String propertyName) |
獲取給定css屬性的值; CSS屬性(如背景、字體、邊框等)可按照DOM CSS2規范獲取; |
示例:獲得“百度一下按鈕”的顏色值; 方法:System.out.println("百度一下按鈕顏色:"+ driver.findElement(By.id("su")).getCssValue("backgroundColor")); |
| Point getLocation() |
返回該元素相對頁面左上角的位置坐標 |
示例:返回“百度一下按鈕”相對頁面左上角的位置坐標 方法:System.out.println("Point:"+ driver.findElement(By.id("su")).getLocation()); |
| Rectangle getRect() |
獲取渲染元素的位置和大小; Chrome & IE可能不支持; |
—— |
| Dimension getSize() |
獲取渲染元素的寬度和高度; |
示例:獲取百度搜索框的寬度和高度 方法:System.out.println("搜索框:"+ driver.findElement(By.id("kw")).getSize()); |
| java.lang.String getTagName() |
獲取該元素的標記名稱; |
示例: 方法:System.out.println("更多產品的tagname:"+ driver.findElement(By.linkText("更多產品")).getTagName()); 場景:<a href="http://www.baidu.com/more/" name="tj_briicon" class="s_bri" target="_blank"> 更多產品</a> |
| java.lang.String getText() |
獲取元素的文本。 |
示例:獲取百度首頁底部的備案顯示; 方法:System.out.println("Text:"+ driver.findElement(By.id("cp")).getText()); |
| boolean isDisplayed() |
判斷當前元素是否顯示; True=顯示,False=隱藏; |
示例:判斷百度首頁底部的備案是否顯示; 方法:System.out.println("Text:"+ driver.findElement(By.id("cp")).isDisplayed()); |
| boolean isEnabled() |
判斷元素是否可用; |
—— |
| boolean isSelected() |
確定該元素是否被選中; 此操作只適用於輸入元素,如復選框、選擇和單選按鈕中的選項。 |
—— |
| void sendKeys ( java.lang.CharSequence... keysToSend ) |
模擬輸入一個元素; |
示例:在百度搜索框中輸入“Selenium; 方法:driver.findElement(By.id("kw")).sendKeys("Selenium"); |
| void submit() |
提交表單給遠程服務器; 注:如果提交導致當前頁面更改,則此方法將阻塞,直到加載到新頁為止。 異常:NoSuchElementException——如果給定元素不在表單內 |
示例:在百度搜索框中輸入“Selenium”,並發送確定搜索請求到服務器; 方法:driver.findElement(By.id("su")).submit(); |
| 以上測試均以 " driver.get("https://www.baidu.com/"); " 為前提; |
||
測試代碼如下:
package com.SeleniumLib.jase; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class WebElementOperate { public static void main(String[]args){ System.out.println("start selenium"); WebDriver driver; System.setProperty("webdriver.chrome.driver","D:/selenium-java-3.5.3/chromedriver.exe"); //chromedriver驅動的本地存放路徑 driver = new ChromeDriver(); driver.get("http://www.baidu.com/"); /*driver.findElement(By.id("kw")).sendKeys("Selenium"); driver.findElement(By.id("su")).click();*/ //清空文本輸入元素的值 /*driver.findElement(By.id("kw")).sendKeys("Selenium"); driver.findElement(By.id("kw")).clear();*/ //定位元素 && 單擊此元素 /*driver.findElement(By.linkText("新聞")).click();*/ //查找所有tagname=input的所有元素 /* List<WebElement> inputs = driver.findElements(By.tagName("input")); //import java.util.List; System.out.println(inputs.size()); */ //獲得“百度搜索輸入框”type屬性的值; /*System.out.println("百度搜索輸入框type屬性:"+driver.findElement(By.id("kw")).getAttribute("type"));*/ //獲取給定css屬性的值 //System.out.println("borderBottomColor:"+driver.findElement(By.id("su")).getCssValue("backgroundColor")); //獲得“百度一下按鈕”的顏色值; //System.out.println("color:"+driver.findElement(By.id("su")).getCssValue("color")); //獲得“百度一下字體”的顏色值; //返回“百度一下按鈕”相對頁面左上角的位置坐標 //System.out.println("Point:"+driver.findElement(By.id("su")).getLocation()); //import org.openqa.selenium.Point; //獲取渲染元素的位置和大小 //----Chrome下測試為ERROR:System.out.println("Rect:"+driver.findElement(By.id("kw")).getRect()); //獲取渲染元素的寬度和高度; //System.out.println("搜索框:"+driver.findElement(By.id("kw")).getSize()); //System.out.println("百度一下按鈕:"+driver.findElement(By.id("su")).getSize()); //獲取元素的標記名稱; //System.out.println("kw的tagname:"+driver.findElement(By.id("kw")).getTagName()); //System.out.println("更多產品的tagname:"+driver.findElement(By.linkText("更多產品")).getTagName()); //場景:<a href="http://www.baidu.com/more/" name="tj_briicon" class="s_bri" target="_blank"> 更多產品</a> //獲取文本元素的文本信息; //System.out.println("Text:"+driver.findElement(By.id("cp")).getText()); //百度底部備案信息 //判斷當前元素是否顯示 //System.out.println("Text:"+driver.findElement(By.id("cp")).isDisplayed()); //True //看到有人說:這個函數用於判斷某個元素是否存在頁面上,這里的存在不是肉眼看到的存在,而是html代碼的存在。某些情況元素的visibility為hidden或者display屬性為none,我們在頁面看不到但是實際是存在頁面的一些元素;目前未證實該說法; //在百度搜索框中輸入“Selenium” //driver.findElement(By.id("kw")).sendKeys("Selenium"); //在百度搜索框中輸入“Selenium”,並發送確定搜索請求到服務器 driver.findElement(By.id("kw")).sendKeys("Selenium"); driver.findElement(By.id("su")).submit(); } }
CSS屬性可參考:http://www.w3school.com.cn/jsref/dom_obj_style.asp#background
Selenium Java API參考路徑:https://seleniumhq.github.io/selenium/docs/api/java/index.html
