Selenium-WebDriver API命令與操作——八種元素定位_selenium

來源:互聯網
上載者:User

原文地址:http://www.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations

(本文只針對python部分翻譯)

首先申明一點:本人英語水平很爛,做此翻譯僅僅用於更深入地去理解webdriver的使用方法。

一、擷取頁面

使用WebDriver的第一步是開啟一個頁面,通常調用get來做這件事:

driver.get("http://www.google.com");
包括作業系統/瀏覽器等諸多因素,WebDriver不會自行等待頁面載入。某些情況下,在頁面還沒有完全載入出來前,WebDriver可能已經失去控制了。為了確保穩定性,可使用“顯式等待”和“隱式等待”,等待所需要定位的元素出現在頁面再進行操作。

顯式等待:

隱式等待:

二、定位元素

在WebDriver中定位元素有兩種方式:通過建立執行個體或者頁面元素。每一種語言都有“Find Element”和“Find Elements”兩種方法。前者返回的是與查詢對應的元素對象,如果該元素沒有發現,則拋出異常。後者返回一個多個頁面元素的列表,如果沒有匹配到查詢的DOM元素,該列表為空白。

Find方法有一個By定位對象,下面列出By的使用方法:

1、By ID

這是定位元素最高效也是首選的方法,常見的問題是UI開發人員做了不唯一或者動態變化的id,這兩種方式都應當避免,為元素設定一個類都比動態id更合適。

例子如下:

HTML:

<div id="coolestWidgetEvah">...</div>
Python代碼:

element = driver.find_element_by_id("coolestWidgetEvah")orfrom selenium.webdriver.common.by import Byelement = driver.find_element(by=By.ID, value="coolestWidgetEvah")

2、By Class Name

本案例中,類即為DOM元素的屬性,通常很多DOM元素的類名是相同的,因此如果尋找一組相同類名的元素,該方法成了更合適的選擇。

例子如下:

HTML:

<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div
Python代碼:

cheeses = driver.find_elements_by_class_name("cheese")orfrom selenium.webdriver.common.by import Bycheeses = driver.find_elements(By.CLASS_NAME, "cheese")

3、By Tag Name

即,DOM元素的標籤名

例子如下:

HTML:

<iframe src="..."></iframe>
Python代碼:

frame = driver.find_element_by_tag_name("iframe")orfrom selenium.webdriver.common.by import Byframe = driver.find_element(By.TAG_NAME, "iframe")
4、By Name

通過匹配元素名稱屬性尋找元素

例子如下:

HTML:

<input name="cheese" type="text"/>
Python代碼:

cheese = driver.find_element_by_name("cheese")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.NAME, "cheese")
5、By Link Text

通過可見文本尋找超連結元素

例子如下:

HTML:

<a href="http://www.google.com/search?q=cheese">cheese</a>>
Python代碼:

cheese = driver.find_element_by_link_text("cheese")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.LINK_TEXT, "cheese")
6、By Partial Link Text

通過可見文本模糊尋找超連結元素

例子如下:

HTML:

<a href="http://www.google.com/search?q=cheese">search for cheese</a>>

Python代碼:

cheese = driver.find_element_by_partial_link_text("cheese")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")
7、By CSS

顧名思義,它是CSS的一種定位策略。通常本地瀏覽器都支援,請通過w3c css selectors擷取常用CSS選取器的列表清單,如果瀏覽器不支援CSS查詢,請使用Sizzle。IE 6、7和ff3.0目前使用Sizzle作為css查詢引擎。

注意,每個瀏覽器不同,對應CSS的版本也不同。

例子如下:

HTML:

<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</
Python:

cheese = driver.find_element_by_css_selector("#food span.dairy.aged")orfrom selenium.webdriver.common.by import Bycheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")

8、By Xpath

WebDriver可以隨時隨地使用Xpath功能,如果瀏覽器不支援Xpath,而我們又這樣做的話,將會導致意想不到的錯誤,除非你懂得不同的XPath引擎之間的差異。

  標籤和屬性名稱 屬性 本地是否支援Xpath HtmlUnit Driver Lower-cased 出現在HTML 是 Internet Explorer Driver Lower-cased 出現在HTML 否 Firefox Driver 不區分大小寫 出現在HTML 是 有點抽象,請看例子:

HTML:

<input type="text" name="example" /><INPUT type="text" name="other" />
Python代碼:

inputs = driver.find_elements_by_xpath("//input")orfrom selenium.webdriver.common.by import Byinputs = driver.find_elements(By.XPATH, "//input")
匹配到的結果(數量)如下:

XPath 運算式 HtmlUnit Driver Firefox Driver Internet Explorer Driver //input 1  2 2 //INPUT 0 2 0 有時候,HTML元素不需要顯式顯示聲明屬性,因為它有一個預設值。例如,一個input標籤不需要一個type屬性,因為它預設為text。在WebDriver中使用xpath的經驗法則是,不要期望能夠匹配到這些隱式屬性。

說明:翻譯中的css與xpath講的很抽象,看的我糊裡糊塗的,其實真實操作中並沒有這麼複雜

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.