一、ID定位
一般情況下頁面元素的id屬性在當前網頁中是唯一的所以使用ID定位可以保證定位的唯一性,不會像其他定位方式一樣可能定位到多個頁面元素。但有的網頁頁面元素沒有id屬性值,導致無法使用ID定位方式。
HTML 源碼
<a onclick="return false;" id="lb" name="tj_login" href="https://passport.baidu.com/v2/?login&tpl=mn&">登錄</a>
Java代碼
WebElement element = driver.findElement(By. id("lb"));
二、name定位
name屬性值在一個網頁中可以不是唯一值,因此使用name方式定位可能會同時定位到多個元素。
HTML 源碼
<a onclick="return false;" id="lb" name="tj_login" href="https://passport.baidu.com/v2/?login&">登錄</a>
java代碼
WebElement element=driver.findElement(By.name("tj_login"));
三、className定位
classname定位可以查找一個或者一組顯示效果相同的頁面元素。
HTML 源碼
<a class="reg" href="https://passport.baidu.com/v2/?reg&u=http%3A%2F%2Fwww.baidu.com%2F">注冊</a>
java代碼
WebElement element = driver.findElement(By.className( "reg"));
四、linkText定位(鏈接全部文字)
此方式定位鏈接需要完全匹配鏈接的顯示文字,常用於頁面中存在多個鏈接文字高度相似的情況,無法使用部分鏈接文字定位。
HTML 源碼
<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索設置</a>
java代碼
WebElement element = driver.findElement(By.linkText( "搜索設置" ));
五、partialLinkText定位(鏈接部分文字)
這種定位方式只需模糊匹配鏈接的顯示文字即可,常用於匹配頁面鏈接文字不定期發生少量變化的情況,使用模糊匹配的方式可以提高鏈接定位的准確率,也可以用於模糊匹配一組鏈接的情況。
HTML 源碼
<a href="http://www.sogou.com">sogou搜索</a><br> <a href="http://www.baidu.com">baidu搜索</a>
Java代碼
WebElement element = driver.findElement(By. partialLinkText( "baidu" ));
List<WebElement> elements=driver.findelement(By.partialLinkText("搜索"));
六、tagname定位(標簽名稱定位)
標簽名定位方式主要用於匹配多個頁面元素的情況,將找到的頁面元素對象進行計數、遍歷。。。
HTML 源碼
<a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索設置</a> <a href="http://www.baidu.com">baidu搜索</a>
Java代碼
WebElement element=driver.findElement(By.tagName("a"));
List<WebElement> elements= driver.findElement(By.tagName(a));
七、XPath定位(這里講述的XPath例子全部是相對路徑定位)
HTML源碼
<html> <head> <title>SeleniumElement</title> <meta http-equiv=Content-Type content="text/html;charset=utf-8"> <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1"> <meta content=always name=referrer> </head> <body> <div id='sousuo'> <a href="http://www.sogou.com">sogou搜索</a><br> <a href="http://www.baidu.com">baidu搜索</a> </div> <br> <div id='ifra' > <td>Input輸入框</td> <input type="text" id="input" name='ips'/> </div> </br> <div id='radio'> <td>RadioBox單選框</td></br> <input type='radio' name="fruit" class='yi'/><label>yi</label></br> <input type='radio' name="fruit" class='er'/><label>er</label></br> <input type='radio' name="fruit" class='san'/><label>san</label></br> <input type='radio' name="fruit" class='si'/><label>si</label></br> <input type='radio' name="fruit" class='wu'/><label>wu</label> </div> <br> </body> </html>
1.使用索引號進行定位,從 1 開始。
java代碼
//通過索引號定位到第2個radio按鈕 WebElement element=driver.findElement(By.xpath("//input[2]"));
2.使用元素屬性值定位
網頁的元素通常包含各種各樣的屬性值,並且很多屬性值具有唯一性若能確認屬性值發生變更的可能性很低且具有唯一值,則推薦使用元素屬性值定位的方法來編寫XPath定位表達式
java代碼
//使用class屬性定位到值為 yi 的按鈕 WebElement yi=driver.findElement(By.xpath("//input[@class='yi']")); //定位到id屬性值為sousuo的div中href屬性值為http://www.baidu.com 鏈接 WebElement ss=driver.findElement(By.xpath("//div[@id='sousuo']/a[@href='http://www.baidu.com']")); //使用type屬性值定位輸入框 WebElement t=driver.findElement(By.xpath("//input[@type='text']"));
3.使用模糊的屬性值定位
自動化實施過程中,會遇到頁面元素屬性值動態的生成,即每次刷新后元素屬性值都會變動。使用模糊的屬性值定位方式可解決一部分此類難題。使用的方法為一下兩個:
> starts-with()
> contains()
java代碼
//查找輸入框id屬性開始位置包含“in”關鍵字的頁面元素 WebElement in=driver.findElement(By.xpath("//input[starts-with(@id,'in')]")); //查找鏈接href屬性包含“baidu”關鍵字的頁面元素 WebElement bu=driver.findElement(By.xpath("//a[contains(@href,'baidu')]"));
4.使用頁面元素的文本來定位
java代碼
//查找元素文本為 baidu搜索 的鏈接
WebElement bd=driver.findElement(By.xpath("//a[text()='baidu搜索']"));
//搜索包含 sogou 的連鏈接
WebElement sg=driver.findElement(By.xpath("//a[contains(text(),'sogou')]"));
5.使用XPath的軸(Axis)進行元素定位
使用XPath軸方式依據在文檔樹種的元素相對位置關系進行定位。先找到一個相對好定位的元素,依據它和要定位元素的相對位置進行定位,可解決一些元素難以定位的問題。提供的XPath軸關鍵字為一下幾種:
① parent:選擇當前節點的上層父節點
② child:選擇當前節點的下層子節點
③ ancestor:選擇當前節點所有上層的節點
④ descestor:選擇當前節點所有上層的節點
⑤ following:選擇在當前節點之后顯示的所有節點
⑥ following-sibling:選擇當前節點的所有平級節點
⑦ preceding:選擇當前節點前面的所有節點
⑧ preceding-sibling:選擇當前節點前面的所有同級節點
XPath相對路徑