一、selenium 定位元素
在使用selenium webdriver進行元素定位時,通常使用findElement或findElements方法結(jié)合By類返回的元素句柄來定位元素。其中By類的常用定位方式共八種,現(xiàn)分別介紹如下:
1、By.name()
# 源碼:
<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba">
<span id="gbqfsa">Google Search</span>
</button>
# 示例:
driver.findElement(By.name("btnK"));
2、By.id()
# 源碼:
<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba">
<span id="gbqfsa">Google Search</span>
</button>
# 示例:
driver.findElement(By.id("gbqfba"));
3、By.tagName()
該方法可以通過元素的標簽名稱來查找元素。該方法跟之前兩個方法的區(qū)別是,這個方法搜索到的元素通常不止一個,所以一般建議結(jié)合使用findElements方法來使用。比如我們現(xiàn)在要查找頁面上有多少個button,就可以用button這個tagName來進行查找,代碼如下:
# 源碼:
<button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba">
<span id="gbqfsa">Google Search</span>
</button>
# 示例:
List<WebElement> buttons = driver.findElements(By.tagName("button")); //返回的是一個集合
4、By.className()
# 源碼:
<button name="sampleBtnName" id="sampleBtnId" class="buttonStyle">I'm Button</button>
# 示例:
driver.findElement(By.className("buttonStyle"));
5、By.linkText()
這個方法比較直接,即通過超文本鏈接上的文字信息來定位元素,這種方式一般專門用于定位頁面上的超文本鏈接。通常一個超文本鏈接會長成這個樣子:
# 源碼:
<a href="/intl/en/about.html">About Google</a>
# 示例:
driver.findElement(By.linkText("About Google"));
6、By.partialLinkText()
這個方法是上一個方法的擴展。當你不能準確知道超鏈接上的文本信息或者只想通過一些關(guān)鍵字進行匹配時,可以使用這個方法來通過部分鏈接文字進行匹配。代碼如下:
# 源碼:
<a href="/intl/en/about.html">About Google</a>
# 示例:
driver.findElement(By.partialLinkText("About"));
7、By.xpath()
這個方法是非常強大的元素查找方式,使用這種方法幾乎可以定位到頁面上的任意元素。在正式開始使用XPath進行定位前,我們先了解下什么是XPath。XPath是XML Path的簡稱,由于HTML文檔本身就是一個標準的XML頁面,所以我們可以使用XPath的語法來定位頁面元素。
# 源碼:
<html>
<body>
<form id="loginForm">
<input name="username" type="text"/>
<input name="password" type="password"/>
<input name="continue" type="submit" value="Login"/>
<input name="continue" type="button" value="Clear"/>
</form>
</body>
</html>
# 示例:找到第三個 input屬性
driver.findElement(By.xpath("http://input[3]"));
下面是相對路徑的引用寫法:
查找頁面根元素://
查找頁面上所有的input元素://input
查找頁面上第一個form元素內(nèi)的直接子input元素(即只包括form元素的下一級input元素,使用絕對路徑表示,單/號)://form[1]/input
查找頁面上第一個form元素內(nèi)的所有子input元素(只要在form元素內(nèi)的input都算,不管還嵌套了多少個其他標簽,使用相對路徑表示,雙//號)://form[1]//input
查找頁面上第一個form元素://form[1]
查找頁面上id為loginForm的form元素://form[@id='loginForm']
查找頁面上具有name屬性為username的input元素://input[@name='username']
查找頁面上id為loginForm的form元素下的第一個input元素://form[@id='loginForm']/input[1]
查找頁面具有name屬性為contiune并且type屬性為button的input元素://input[@name='continue'][@type='button']
查找頁面上id為loginForm的form元素下第4個input元素://form[@id='loginForm']/input[4]
8、By.cssSelector()
下面是一些常見的cssSelector的定位方式:
定位id為flrs的div元素,可以寫成:#flrs 注:相當于xpath語法的//div[@id=’flrs’]
定位id為flrs下的a元素,可以寫成 #flrs > a 注:相當于xpath語法的//div[@id=’flrs’]/a
定位id為flrs下的href屬性值為/forexample/about.html的元素,可以寫成: #flrs > a[href=”/forexample/about.html”]
如果需要指定多個屬性值時,可以逐一加在后面,如#flrs > input[name=”username”][type=”text”]。
# 源碼:
<html>
<body>
<form id="loginForm">
<input name="username" type="text"/>
<input name="password" type="password"/>
<input name="continue" type="submit" value="Login"/>
<input name="continue" type="button" value="Clear"/>
</form>
</body>
</html>
# 示例:找到第三個 input屬性
driver.findElement(By.cssSelector(" input[type=submit] "));