selenium 找元素的方法

一、找元素的方法By方法

1、By.id:一般都是唯一的  eg:id = name

2、By.name:頁面若有多個name,則會選中第一個name  eg:name = name

3、By.className:     eg:class =name

Selenium WebDriver提供了findElement()方法來定位頁面中需要測試的元素。當開始尋找符合指定條件元素時,它將會查詢整個DOM,然後返回第一個找到的匹配的元素

More:

如果頁面上有一些重複的元素。但是他們在不同的<div>中。需要先定位父元素<div>,再定位其子元素,方法如下:

4、WebElement div =driver.findElement(By.id(“div1”));

WebElement topLink = div.findElement(By.linkText(“top”));

 

也可以縮寫成一行:

WebElement topLink =driver.findElement(By.id(“div1”)).findElement(By.linkText(“top”))

 

5、By.linkText:

通過部分鏈接名稱定位鏈接:

WebElement inboxLink =driver.findElement(By.partialLinkText(“inbox”));

System.out.println(inboxlink.getText());

6、By.tagName()和getElementByTagName()很相似

如果想統計<table>中郵多少行,可以這樣做

WebElement table =driver.FindElement(By.id(“summaryTable”));

List<WebElement> rows =table.findElement(By.tagName(“tr”));

assertEquals(10,rows.size());

 

7、By.linkText()

WebElement gmailLink = driver.findElement(By.linkText(“Gmail”));

assertEquals(“http://mail.google.com/”,gmailLink.getAttribute(“href”));

 

8、通過部分鏈接名稱定爲鏈接

PartialLinkTest()來定位固定的已知不變的一部分名稱

WebElement inboxLink =driver.findElement(By.partialLinkText(“Inbox”));

System.out.println(inboxLink.getText());

9、cssSelector這種方式,有限制,取決於頁面的整個結構。若有改變,則找不到該元素。

By.cssSelector(“html>body>div>div>form>input”)

使用相對路徑來定位元素

可以用class屬性定爲元素,先指定一個HTML標籤,加一個.符號,跟上class屬性的值。

WebElement loginButton = driver.findElement(By.cssSelector(“input.login”));

                                                                                     (By.cssSelector(“.login”));

此方法,與By.className()很相似

用相對ID選擇器來定位元素.可以先指定一個HTML標籤,再加上一個#,跟上id的屬性值

WebElement username =driver.findElement(By.cssSelectro(“input#userName”))

                                                                           (By.CssSelector(“#userName”));這裏跟id選擇器很像。

使用input中的name屬性定爲。

WebElement username =driver.findElement(By.cssSelector(“input [name=username]”))

類似於By.name()方法

定位<img>元素

WebElement previousButton =driver.findElement(By.cssSelector(“img [alt = ‘Previous’]”));

使用多個屬性定位<input>元素

WebElement previousButton =driver.findElement(By.cssSelector(“input [type = ‘submit’][value = ‘login’]”))


使用屬性名稱選擇器來定爲元素。

與之前的不同,我們只通過指定元素中屬性的名稱而不是屬性的名字來定爲元素。

List<WebElement> imageWithAlt =driver.findElements(By.cssSelector(“img[alt]”));

Not()僞類也使用來匹配不滿足規則的元素。如,想要定位<img>標籤中不含alt屬性

方法如下:

List<WebELlment> imageWithoutAlt =driver.findElements(By.cssSelector(“img:not([alt])”))

一、使用xpath定位元素

1、  使用絕對路徑定位用戶名字段,在每個元素之間需要有一個空格

WebElement username= driver.findElement(By.xpath(“html/body/div/div/form/input”))

通過相對路徑                                                   (By.xpath(“//input”));

2、  通過索引定位元素。找到第二個<input>

WebElement username = driver.findElement(By.xpath(“//input[2]”))

3、  xpath中使用元素的屬性定位元素。如ID

WebElement passwd = driver.findElement(By.xpath(“//input[@id=’username’]”))

         使用alt屬性定位image

            WebElement previousButton =driver.findElement(By.xpath(“//input[@alt = ‘previous’]”))

使用多個屬性來定位<input>元素,可以使用or或and

WebElement previousButton =

driver.findElement(By.xpath(“//input[@type= ‘submit’][@value = ‘login’]”))

4、  使用指定元素中屬性的名稱而不是屬性的值來定位元素。Eg:

List<WebElement> imageWithAlt =driver.findElements(By.xpath(“img[@alt]”));


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