学习总结——Selenium元素定位
讀一本好書,不能讀讀就算了,做一下總結,變成自己的,以備查閱。
1.???????? driver.findElement(By.id(<element ID>))
ID是獨一無二的,使用ID定位是最為推薦的方法。
但是:1.不是所有元素都會指定ID;2.有的ID屬性的值是動態生成的。
2.???????? driver.findElement(By.name(<element name>))
name屬性不一定唯一,如果有多個,第一個出現的會被選擇。
3.???????? driver.findElement(By.className(<element class>))
4.???????? driver.findElement(By.tagName(<htmltagname>))
tagName()方法是定位 HTML 標記名稱:
WebElement table = driver.findElement(By.id("summaryTable"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
assertEquals(10, rows.size());
5.???????? driver.findElement(By.linkText(<linktext>))
WebElement 類也可以支持查詢子類元素。
WebElement topLink = driver.findElement(By.id("div1")).findElement(By.linkText("top"));
6.???????? driver.findElement(By.partialLinkText(<linktext>))
當元素有部分在變時,可以用部分不變的內容來定位元素。
7.???????? driver.findElement(By.cssSelector(<cssselector>))
1)???????? 絕對路徑如:WebElement userName = driver.findElement(By.cssSelector("html >
???????? body > div > div > form > input"));
但是,這個策略會有一些的限制,他取決于頁面的整個結構。如果有些許改變,選擇???? 器將找不到這個元素。
??????? 相對路徑
2)???????? DOM中第一個<input>元素:WebElement userName = driver.findElement(By.cssSelector("input"));
3)???????? ?使用class定位:先指定一個 HTML 的標簽,然后加一個“.”符號,跟上 class 屬性的值, WebElement loginButton =driver.findElement(By.cssSelector("input.login"));可以找到按鈕的<input>標簽 class 為 login 的元素。
使用ID 來定位:先指定一個 HTML 標簽,然后加上一個“#”符號,跟上 id 的屬性? 值,如下所示:
WebElement userName =driver.findElement(By.cssSelector("input#username"));
???????? 這將會返回 input 標簽中 id 為 username 的元素。
使用name定位:WebElement userName =
driver.findElement(By.cssSelector("input[name=username]"));
???????? 使用 name 屬性來定位元素和直接用 By 類中的 name()方法來定位相似。?????
???????? 使用其他的屬性定位:WebElement previousButton ????? driver.findElement(By.cssSelector("img[alt='Previous']"));
4)???????? 使用多個屬性來定位<input>元素:WebElement previousButton =driver.findElement(By.cssSelector("input[type='submit'][value='Login']"));
5)???????? 使用屬性名稱定位元素:List<WebElement> imagesWithAlt =
???????? driver.findElements(By.cssSelector("img[alt]"));
not()偽類匹配不滿足規則的元素: 例如, 想要定位那些<img>標簽中不含有alt屬性,
???????? List<WebElement> imagesWithoutAlt=driver.findElements(By.cssSelector("img:not([alt])"));
6)???????? 部分屬性值的匹配:
???????? input[id^= ' ctrl']:以ctrl開始
???????? input[id$='_userName']:以_userName結尾
???????? input[id*='userName']:包含userName
8.???????? driver.findElement(By.xpath(<xpath queryexpression>))
1)???????? 絕對路徑:WebElement userName =driver.findElement(By.xpath("html/body/div/div/form/input"));
???????? 這個策略有局限性,他需要參考整個頁面的文檔結構。如改變了,此元素的定位將會
???????? 失效。
相對路徑:
2)???????? DOM中第一個<input>元素:
???????? WebElement userName = driver.findElement(By.xpath("//input"));
???????? 使用索引來定位元素,第二個<input>:
???????? WebElement passwd = driver.findElement(By.xpath("//input[2]"));
3)???????? 用 ID 屬性來定位:
???????? WebElement userName =driver.findElement(By.xpath("//input[@id='username']"));
使用 alt 屬性來定位:
???????? WebElement previousButton = driver.findElement(By.xpath("img[@alt='Previous']"));
4)???????? 使用多個屬性來定位<input>元素:
- WebElement previousButton =driver.findElement(By.xpath("//input[@type='submit'][@value='Login']"));
- WebElement previousButton = driver.findElement(By.xpath("//input[@type='submit'and @value='Login']"));
- WebElement previousButton = driver.findElement(By.xpath("//input[@type='submit'or @value='Login']"));
5)???????? 使用 XPath 及屬性名稱定位元素:
???????? List<WebElement> imagesWithAlt = driver.findElements(By.xpath ("img[@alt]"));
6)???????? 部分屬性值的匹配:
???????? input[starts-with(@id,'ctrl')]:id以ctrl開始
???????? input[ends-with(@id,'_userName')]:id以_userName結束
???????? Input[contains(@id,'userName')]:id包含userName
7)???????? 使用值來匹配任意屬性及元素:
???????? WebElement userName = driver.findElement(By.xpath("//input[@*='username']"));
8)???????? 使用 XPath 軸來定位元素:用到再研究。
?
總結提示:
轉載于:https://www.cnblogs.com/lucy-lily/p/5158144.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的学习总结——Selenium元素定位的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (简单) POJ 3984 迷宫问
- 下一篇: poj 2886 Who Gets th