※元素定位的重要性:在於查找元素 And 執行元素css
定位元素的三種方法html
findElement和findElements方法node
findElement()返回一個WebElement元素 web
findElements()返回一個List,多個WebElement元素chrome
八種定位方式ui
•By.id(id):經過ID 屬性查找url
•By.name(name):經過name屬性查找spa
•By.className(className) :經過classname屬性查找插件
•By.linkText(連接文本):經過連接文本code
•By.partialLinkText(部分連接文本):經過部分連接文本
•By.cssSelector(Css路徑):經過CSS路徑
•By.tagName(name):經過tagname查找
•By.xpath(XPath路徑):經過XPath查找
例如:id\name\classname
代碼以下:
import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class ZmbxTestJC { @Test public void Testid() throws InterruptedException { WebDriver driver =new ChromeDriver(); String url = "https://www.baidu.com"; driver.get(url); driver.findElement(By.name("wd")).sendKeys("Selenium");//定位到name,輸入內容 driver.findElement(By.id("su")).click();//定位到id,點擊百度一下查詢內容 driver.quit(); } }
例如:By.linkText(Link文本)
HTML 源碼: <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索設置</a> WebElementelement =driver.findElement(By.linkText( "搜索設置" ));
例如:By.partialLinkText(部分連接文本)
HTML 源碼: <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索設置</a> WebElementelement =driver.findElement(By. partialLinkText( "搜索" ));//搜索設置
例如:By.cssSelector(Css路徑)
HTML 源碼: <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新聞</a> WebElementelement=driver.findElement(By. cssSelector( "#u1 > a:nth-child(1)" ));
例如:By.tagName(name)
HTML 源碼: <a name="tj_setting" href="http://www.baidu.com/gaoji/preferences.html">搜索設置</a> WebElementelement=driver.findElement(By. tagName( "a" ));
例如:By.xpath(XPath路徑)
HTML 源碼: <a href="http://news.baidu.com" name="tj_trnews" class="mnav">新聞</a> WebElementelement=driver.findElement(By. xpath( "//*[@id="u1"]/a[1]" ));
注意:
1.使用findElement()方法查找元素,元素必須是惟一
2.findElements()一樣支持這八種定位方式,只是獲取的是多個元素,返回List
XPath 是一門在 XML 文檔中查找信息的語言。XPath 用於在 XML 文檔中經過元素和屬性進行導航。
在 XPath 中,有七種類型的節點:元素、屬性、文本、命名空間、處理指令、註釋以及文檔節點(或稱爲根節點)。XML 文檔是被做爲節點樹來對待的。樹的根被稱爲文檔節點或者根節點。
請看下面這個 XML 文檔:
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book> </bookstore>
上面的XML文檔中的節點例子:
<bookstore> (文檔節點)
<author>J K. Rowling</author> (元素節點)
lang="en" (屬性節點)
XPath 使用路徑表達式在 XML 文檔中選取節點。節點是經過沿着路徑或者 step 來選取的。
表達式 | 描述 |
---|---|
nodename | 選取此節點的全部子節點。 |
/ | 從根節點選取。 |
// | 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。 |
. | 選取當前節點。 |
.. | 選取當前節點的父節點。 |
@ | 選取屬性。 |
路徑表達式 | 結果 |
---|---|
bookstore | 選取 bookstore 元素的全部子節點。 |
/bookstore | 選取根元素 bookstore。 註釋:假如路徑起始於正斜槓( / ), 則此路徑始終表明到某元素的絕對路徑! |
bookstore/book | 選取屬於 bookstore 的子元素的全部 book 元素。 |
//book | 選取全部 book 子元素,而無論它們 在文檔中的位置。 |
bookstore//book | 選擇屬於 bookstore 元素的後代的所 有 book 元素,而無論它們位於 bookstore 之下的什麼位置。 |
//@lang | 選取名爲 lang 的全部屬性。 |
軸可定義相對於當前節點的節點集。
軸名稱 | 結果 |
---|---|
ancestor | 選取當前節點的全部先輩(父、祖父等)。 |
ancestor-or-self | 選取當前節點的全部先輩(父、祖父等)以及當前節點自己。 |
attribute | 選取當前節點的全部屬性。 |
child | 選取當前節點的全部子元素。 |
descendant | 選取當前節點的全部後代元素(子、孫等)。 |
descendant-or-self | 選取當前節點的全部後代元素(子、孫等)以及當前節點自己。 |
following | 選取文檔中當前節點的結束標籤以後的全部節點。 |
namespace | 選取當前節點的全部命名空間節點。 |
parent | 選取當前節點的父節點。 |
preceding | 選取文檔中當前節點的開始標籤以前的全部節點。 |
preceding-sibling | 選取當前節點以前的全部同級節點。 |
self | 選取當前節點。 |
詳細瞭解xpath,請參照 https://www.w3school.com.cn/xpath/xpath_summary.asp