[跟著官方文檔學(xué)Selenium][學(xué)習(xí)筆記](méi)[六][WebDriver的元素]
交互
用于操縱表單的高級(jí)指令集。
僅有五種基本命令可用于元素的操作:
點(diǎn)擊(適用于任何元素)
發(fā)送鍵位(僅適用于文本字段和內(nèi)容可編輯元素)
清除(僅適用于文本字段和內(nèi)容可編輯元素)
提交(僅適用于表單元素)
選擇(參見選擇列表元素)
附加驗(yàn)證
這些方法的設(shè)計(jì)目的是盡量模擬用戶體驗(yàn),所以,與Actions接口不同,在指定制定操作之前,會(huì)嘗試執(zhí)行兩件事。
1,如果它確定元素在視圖之外,則會(huì)將元素滾動(dòng)到視圖中,特別是將元素底部與視圖底部對(duì)齊。
2, 確保元素在執(zhí)行操作之前是可交互的。這可能意味著滾動(dòng)不成功,或者該元素沒(méi)有以其他方式顯示。確定某個(gè)元素是否顯示在頁(yè)面上太難了無(wú)法直接在webdriver規(guī)范中定義,因此Selenium發(fā)送一個(gè)帶有JavaScript原子的執(zhí)行命令,檢查是否有可能阻止該元素顯示。如果確定某個(gè)元素不在視圖中,不顯示,不可鍵盤交互,或不可指針交互,則返回一個(gè)元素不可交互錯(cuò)誤。
點(diǎn)擊
元素點(diǎn)擊命令執(zhí)行在元素中央。如果元素中央由于某些元素被遮擋,Selenium將返回一個(gè)元素點(diǎn)擊中斷錯(cuò)誤。
發(fā)送鍵位
元素發(fā)送鍵位命令將錄入提供的鍵位到可編輯的元素。通常,這意味著元素是具有文本類型的表單的輸入元素或具有內(nèi)容可編輯屬性的元素。如果不可編輯,則返回?zé)o效元素狀態(tài)錯(cuò)誤。以下是WebDriver支持的按鍵列表。
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class HelloSelenium { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { //輸入U(xiǎn)RL webDriver.get("https://www.baidu.com"); //輸入Selenium按下回車鍵 webDriver.findElement(By.id("kw")).sendKeys("Selenium" + Keys.ENTER); } finally { webDriver.quit(); } } }
清除
元素清除命令重置元素的內(nèi)容。這要求元素可編輯,且可重置。通常,這意味著元素是具有文本類型的表單的輸入元素或具有內(nèi)容可編輯屬性的元素。如果不滿足這些條件,將返回?zé)o效元素狀態(tài)錯(cuò)誤。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; public class clear { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { //導(dǎo)航URL webDriver.get("https://www.baidu.com"); //存儲(chǔ)SearchInput元素 WebElement searchInput = webDriver.findElement(By.id("kw")); searchInput.sendKeys("Selenium"); //清除輸入的文本 searchInput.clear(); } finally { //webDriver.quit(); } } }
提交
在Selenium4中,不再通過(guò)單獨(dú)的端點(diǎn)以及腳本執(zhí)行的方法來(lái)實(shí)現(xiàn)。因此,建議不要使用此方法,而是單擊相應(yīng)的表單提交按鈕。
查詢器
根據(jù)提供的定位值定位元素。
使用Selenium的最基本方面之一是獲得要使用的元素引用。Selenium提供了許多內(nèi)置的定位器策略來(lái)唯一地識(shí)別元素。在高級(jí)方法中,可以通過(guò)許多方式使用這些定位器。出于本文檔的目的,讓我們考慮一下這段HTML代碼:
- patatoes
- onions
- Tomato is a vegetable
- bananas
- apples
- Tomato is a fruit
第一個(gè)匹配的元素
很多定位器會(huì)匹配頁(yè)面的多個(gè)元素。查找單個(gè)元素方法將返回給給定上下文中找到的第一個(gè)元素的引用。
評(píng)估整個(gè)DOM
當(dāng)find element方法被驅(qū)動(dòng)實(shí)例調(diào)用時(shí),它將返回在DOM中與提供的定位器相匹配的第一個(gè)元素。這個(gè)值可以被存儲(chǔ)或元素操作所使用。在上面的HTML例子中,有兩個(gè)元素,其中一個(gè)叫tomatoes,所以這個(gè)方法將返回vegetables列表中的元素。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; public class Demo1 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\demo1.html"); WebElement vegetable = webDriver.findElement(By.className("tomatoes")); System.out.println(vegetable.getText());//Tomato is a vegetable } }
評(píng)估DOM的子集
相比在整個(gè)DOM中查找唯一定位器,經(jīng)常會(huì)用到查找的是定位另一個(gè)元素。在上面例子中,兩個(gè)元素的類名都為"tomatoes",要找到第二個(gè)的引用有點(diǎn)挑戰(zhàn)性。
一種解決方案是查找具有唯一屬性的元素,該元素是所需元素的祖先,而不是不需要的元素的祖先,然后在該對(duì)象上調(diào)用find element。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; public class Demo2 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\demo1.html"); WebElement fruits = webDriver.findElement(By.id("fruits")); WebElement tomatoes = fruits.findElement(By.className("tomatoes")); String text = tomatoes.getText(); System.out.println(text);//Tomato is a fruit } }
優(yōu)化定位器
如上例所示,嵌套查找可能不是最有效的位置策略,因?yàn)檫@需要向?yàn)g覽器發(fā)出兩個(gè)單獨(dú)的指令。
為了稍微提高性能,我們可以使用CSS或者XPath在單條指令中查找元素??梢杂^看我們鼓勵(lì)測(cè)試實(shí)踐部分中的定位器策略建議。
在這個(gè)例子中我們將使用CSS Selector
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; public class Demo3 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\demo1.html"); WebElement fruit = webDriver.findElement(By.cssSelector("#fruits .tomatoes")); System.out.println(fruit.getText());//Tomato is a fruit } }
匹配所有元素
需要獲取與定位器匹配的所有元素(而不僅僅是第一個(gè)元素)的引用有幾種用例。復(fù)數(shù)查找元素方法返回元素引用的集合。如果沒(méi)有匹配項(xiàng),則返回空列表。在這種情況下,所有水果和蔬菜列表項(xiàng)的引用將在集合中返回。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.List; public class Demo4 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\demo1.html"); List
獲取元素
通常,你會(huì)獲得一個(gè)元素集合,但希望使用特定元素,這意味著你需要循環(huán)訪問(wèn)該集合并確定所需的元素。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.List; public class Demo5 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\demo1.html"); List
從元素中找元素
用于在父元素的上下文中查找匹配的子WebElements的列表。為了實(shí)現(xiàn)這一點(diǎn),父WebElement使用"findElements"鏈接來(lái)訪問(wèn)子元素。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import java.util.List; public class findElementsFromElement { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("XXX\example.html"); //通過(guò)tagName 'div' 獲取元素 WebElement element = webDriver.findElement(By.tagName("div")); //通過(guò)tagName 'p' 獲取所有可用元素 List
獲取激活元素
它用于追蹤或查找在當(dāng)前瀏覽上下文中具有焦點(diǎn)的DOM元素
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class activeElementTest { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://www.baidu.com"); webDriver.findElement(By.cssSelector("#kw")).sendKeys("Selenium"); //獲取當(dāng)前激活元素的屬性 String attr = webDriver.switchTo().activeElement().getAttribute("name"); System.out.println(attr);//wd } finally { webDriver.quit(); } } }
信息
你可以查詢有關(guān)特定元素的許多詳細(xì)信息
Is Displayed
此方法用于檢查鏈接的元素是否顯示在網(wǎng)頁(yè)上。返回一個(gè)布爾值,如果鏈接的元素顯示在當(dāng)前瀏覽上下文中就是True,否則為False。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class Demo1 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("https://www.baidu.com"); boolean kwDisplayed = webDriver.findElement(By.id("kw")).isDisplayed(); if (kwDisplayed) { System.out.println("搜索框已顯示"); } else { System.out.println("搜索框沒(méi)有顯示"); } } }
Is Element Selected
此方法確定是否已選擇引用的元素。廣泛用于復(fù)選框,單選按鈕,輸入元素和選項(xiàng)元素。
返回一個(gè)布爾值,如果在當(dāng)前瀏覽上下文中已選擇引用的元素返回True,否則返回False。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class Demo2 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\Demo2.html"); //如果選中元素返回True boolean tomatoSelected = webDriver.findElement(By.name("tomato")).isSelected(); if (tomatoSelected) { System.out.println("西紅柿被選中"); } else { System.out.println("西紅柿沒(méi)被選中"); } boolean appleSelected = webDriver.findElement(By.name("apple")).isSelected(); if (appleSelected) { System.out.println("蘋果被選中"); } else { System.out.println("蘋果沒(méi)被選中"); } } }
Get Element TagName
此方法用于獲取在當(dāng)前瀏覽上下文中具有焦點(diǎn)的被引用的元素TagName
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class Demo3 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("https://www.example.com"); String h1Value = webDriver.findElement(By.cssSelector("h1")).getTagName(); System.out.println(h1Value); } }
Get Element Rect
用于獲取參考元素的尺寸和坐標(biāo)
提取的數(shù)據(jù)主體包含以下詳細(xì)信息:
元素左上角的X軸位置
元素左上角的Y軸位置
元素高度
元素寬度
import org.openqa.selenium.By; import org.openqa.selenium.Rectangle; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class Demo4 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("https://www.example.com"); //返回高寬xy四個(gè)元素 Rectangle res = webDriver.findElement(By.cssSelector("h1")).getRect(); //Rectangle類提供了獲取xy高寬方法 System.out.println(res.getX()); System.out.println(res.getY()); System.out.println(res.getWidth()); System.out.println(res.getHeight()); } }
獲取元素CSS值
獲取當(dāng)前瀏覽上下文中元素的特定計(jì)算樣式屬性的值
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class Demo5 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("https://www.example.com"); String color = webDriver.findElement(By.linkText("More information...")).getCssValue("color"); System.out.println(color);//rgba(56, 72, 143, 1) } }
獲取元素文本
獲取特定元素渲染后的文本
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; public class Demo6 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("https://www.example.com"); String text = webDriver.findElement(By.cssSelector("h1")).getText(); System.out.println(text);//Example Domain } }
定位器
在DOM中標(biāo)識(shí)一個(gè)或多個(gè)特定元素的方法。定位器是一種標(biāo)識(shí)頁(yè)面上元素的方法。它是傳遞給查找元素方法的參數(shù)。
元素選擇策略
在WebDriver中有8種不同的內(nèi)置元素定位策略:
相對(duì)定位器
Selenium 4引入了相對(duì)定位器(以前稱為友好定位器)。當(dāng)為所需元素構(gòu)建定位器并不容易,但很容易在空間上描述元素相對(duì)于具有易于構(gòu)建的定位器的元素位置時(shí),這些定位器非常有用。
相對(duì)定位器如何工作的
Selenium使用JavaScript函數(shù)getBoundingClientRect()定義頁(yè)面元素的大小和位置,并且可以用這些信息定位旁邊的元素。
相對(duì)定位器方法可以將原點(diǎn)(先前定位的元素參考或者其他定位器)用作原點(diǎn)的參數(shù)。
讓我們以下面的頁(yè)面為例來(lái)理解相對(duì)定位器。
可用的相對(duì)定位器
如果email的文本域不容易獲取,但是密碼文本域容易獲取,我們可以定位"input"元素在密碼元素"上方"
By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password"));
如果密碼文本域不容易獲取,但是email的文本域容易獲取,我們可以定位"input"元素在email元素"下方"
By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email"));
如果cancel按鈕不容易獲取,但是submit按鈕容易獲取,我們可以通過(guò)定位submit元素的左邊來(lái)找到cancel元素
By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit"));
如果submit按鈕不容易獲取,但是cancel按鈕容易獲取,我們可以通過(guò)定位cancel元素的右邊來(lái)找到submit元素
By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel"));
如果相對(duì)定位不明顯,或者因窗口大小而已,則可以使用near方法識(shí)別距離,提供的定位器范圍最多50px.一個(gè)很好的用例是使用一個(gè)表單元素,該元素沒(méi)有易于構(gòu)建的定位器,但其關(guān)聯(lián)的輸入標(biāo)簽元素具有
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
鏈?zhǔn)较鄬?duì)定位器
需要的話你還可以鏈接定位器。有時(shí),該元素最容易被識(shí)別為一個(gè)元素的上/下以及另一個(gè)元素的左/右。
By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email").toRightOf(By.id("cancel"));
選擇列表
與其他元素相比,選擇列表具有特俗的行為。
選擇元素可能需要大量樣板代碼才能自動(dòng)化。為了減少這種情況并使你的測(cè)試更干凈,在Selenium的support包中有一個(gè)Select類。要使用它需要導(dǎo)入以下語(yǔ)句:
import org.openqa.selenium.support.ui.Select;
然后,能夠參考
WebElement selectElement = driver.findElement(By.id("selectElementID")); Select selectObject = new Select(selectElement);
Select對(duì)象現(xiàn)在將為你提供一系列命令,使你可以與
有三種方法可以從上述元素中選擇第一個(gè)選項(xiàng):
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.Select; public class Demo1 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\\demo.html"); WebElement selectElement = webDriver.findElement(By.id("selectElementID")); Select selectObject = new Select(selectElement); //根據(jù)下標(biāo)選擇 //selectObject.selectByIndex(1); //根據(jù)值的屬性選擇 //selectObject.selectByValue("value1"); //根據(jù)text選擇 selectObject.selectByVisibleText("Bread"); } }
然后,可以檢視所有被選擇的選項(xiàng):
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.Select; import java.util.List; public class Demo2 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\\demo.html"); WebElement selectElement = webDriver.findElement(By.id("selectElementID")); Select selectObject = new Select(selectElement); //將選項(xiàng)返回到List集合 List
或者你可能只對(duì)
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.Select; import java.util.List; public class Demo3 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\\demo.html"); WebElement selectElement = webDriver.findElement(By.id("selectElementID")); Select selectObject = new Select(selectElement); List
如果要取消選擇任何元素,現(xiàn)在有四個(gè)選項(xiàng):
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.Select; public class Demo4 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\\demo.html"); WebElement selectElement = webDriver.findElement(By.id("selectElementID")); Select selectObject = new Select(selectElement); //1,根據(jù)下標(biāo)刪除 selectObject.deselectByIndex(1); //2,根據(jù)值屬性刪除 selectObject.deselectByValue("value1"); //3,根據(jù)text刪除 selectObject.deselectByVisibleText("Bread"); //4,刪除所有 selectObject.deselectAll(); } }
最后,一些
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.support.ui.Select; public class Demo5 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("XXX\\demo.html"); WebElement selectElement = webDriver.findElement(By.id("selectElementID")); Select selectObject = new Select(selectElement); boolean doesThisAllowMultipleSelections = selectObject.isMultiple(); System.out.println(doesThisAllowMultipleSelections);//false } }
Java Selenium
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。