webdriver中元素查找常用方法

Selenium尋找元素定位
id
findElement(by.id(“id”))
name
findElement(by.name(“name”))
class_name
findElement(by.className(“className”))
linkText
driver.findElement(By.linkText(“Inbox”));
partialLinkText
driver.findElement(By.partialLinkText(“Inbox”));
tagName
tagName(“html的類別信息如button”)
Css
1)絕對路徑 在DOM中的具體位置
findElement(by.cssSelector(“html body div form input”))
或findElement(by.cssSelector(“html>body>div>form>input”))
2)相對路徑
driver.findElement(By.cssSelector(“input”));第一個input元素。
driver.findElement(By.cssSelector(“input.login”)); html標籤.class的屬性值
3)相對Id選擇器
driver.findElement(By.cssSelector(“input#username”));html標籤#id
driver.findElement(By.cssSelector(“#username”));只是#id
4)屬性
driver.findElement(By.cssSelector(“input[name=username]”));使用name屬性
driver.findElement(By.cssSelector(“img[alt=’Previous’]”));使用alt屬性
driver.findElements(By.cssSelector(“img[alt]”));通過屬性名稱查找,頁面所有img含 有alt屬性的標籤
driver.findElement(By.cssSelector(“input[type=’submit’][value=’Login’]”));聯合多個屬 性查詢
driver.findElements(By.cssSelector(“img:not([alt])”));使用僞類
5)部分屬性 (對於頁面上有動態變化的屬性的元素是非常有用的)
^= driver.findElement(By.cssSelector(Input[id ^ =‘ ctrl’]));匹配到id頭部 如ctrl_12
$= driver.findElement(By.cssSelector(Input[id ^ =‘ ctrl’]));匹配到id尾部 如a_ctrl
= driver.findElement(By.cssSelector(Input[id =‘ ctrl’]));匹配到id中間如1_ctrl_12
高級CSS
1)查詢子元素
WebElement userName = driver.findElement(By.cssSelector(“form#loginForm > input”));
WebElement userName = driver.findEleme(By.cssSelector(“form#loginForm :nth-child(2)”));
:first-child 定位表單第一個子元素
:last-child 定位表單最後一個子元素
:nth-child(2) 定位表單中第二個子元素
2)使用僞類
driver.findElement(By.cssSelector(“input:focus”)); 也可使用hover active
:enable input:enable 定位屬性爲enable的input元素
:disable input:disable 定位屬性爲disable的input元素
:checked input:checked 定位有多選框屬性爲checked的元素
3)查詢兄弟元素
driver.findElement(By.cssSelector(“#nv a + b”)); 定位到a 再定位到和它相鄰的b
Xpath(可以向前向後查詢DOM結構,css只能向前)
1)絕對路徑
driver.findElement(By.xpath(“html/body/div/div/form/input”));//如果發生結構改變則 找不到
2)相對路徑
driver.findElement(By.xpath(“//input”));//假設在DOM中的第一個
3)使用索引
driver.findElement(By.xpath(“//input[2]”));//找第二個input元素
4)屬性值
driver.findElement(By.xpath(“//input[@id=’username’]”));//使用id屬性匹配
driver.findElement(By.xpath(“img[@alt=’Previous’]”));//使用alt屬性
driver.findElement(By.xpath (“//input[@type=’submit’][@value=’Login’]”));//聯合多個屬性
WebElement previousButton = driver.findElement (By.xpath(“//input[@type=’submit’and @value=’Login’]”));//使用and聯合查詢
WebElement previousButton = driver.findElement (By.xpath(“//input[@type=’submit’or @value=’Login’]”));//使用or選擇查詢
5)屬性名稱
List imagesWithAlt = driver.findElements (By.xpath (“img[@alt]”));//使用屬 性名稱 img中帶有alt屬性的元素
6)部分屬性值
starts-with() driver.findElement(By.XPath(“input[starts-with(@id,’ctrl’)]”));
ends-with() driver.findElement(By.XPath(“input[ends-with(@id,’ctrl’)]”));
contains() starts-with() driver.findElement(By.XPath(“input[contains(@id,’ctrl’)]”));
7)使用值匹配任意元素屬性值
driver.findElement(By.xpath(“//input[@*=’username’]”));任意屬性名稱爲username的元素
8)XPath軸 借住於元素與元素之間的關係定位
ancestor //td[text()=’Product1’]/ancestor::table 選擇當前節點所有的父類元素
屬性 名稱 元素
descendant //table/descendant::td/input 選擇當前節點所有子元素
following //td[text()=’Product1’]/following::tr 選擇當前元素結束標籤後的所有元素
following-sibling //td[text()=’Product1’]/following-sibling::td 當前元素後的兄弟元素
preceding //td[text()=’150]/preceding::trprecedingsibling//td[text()= 150’]/preceding-sibling::td 當前借點之前的所有同級節點
定位單元格元素
方式:
table:定義表格
caption:表格標題
th:表頭
tr:行
td:單元
thead:頁眉
tbody:主題
tfoot:頁腳
col:列的屬性
colgroup:列的組

findElement將會查詢整個DOM 最終返回第一個找到的匹配的元素
findElement可以查詢子類,縮寫爲
driver.findElement(By.id(“div1”)).findElement(By.linkText(“top”));
查找一個元素 查找這個元素下的子類top
當findElement找不到元素時。拋出NoSuchElementFoundException
findElements()方法返回所有匹配定位策略的WebElement的集合,我們可以使用java中List類來創建WebElements的實例,實現查找多個元素:
List links = driver.findElements(By.cssSelector(“#nv a”));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章