selenium+python自動化,元素定位


webdriver 提供了八種元素定位方法:
  •  id

  •  name

  •  class name

  •  tag name

  •  link text

  •  partial link text

  •  xpath

  •  css selector

  • Python 語言中對應的定位方法如下:

  • find_element_by_id()

  • find_element_by_name()

  • find_element_by_class_name()

  • find_element_by_tag_name()

  • find_element_by_link_text()

  • find_element_by_partial_link_text()

  • find_element_by_xpath()

  • find_element_by_css_selector()

1.使用xpath定位:

1)絕對路徑:

find_element_by_xpath() 方法用於 XPath 語言定位元素。XPath 的絕對路徑主要用標簽名的層級關系來 定位元素的絕對路徑。最外層為 html 語言,body 文本內,一級一級往下查找,如果一個層級下有多個相 同的標簽名,那么就按上下順序確定是第幾個,div[2]表示第二個 div 標簽。

 例:find_element_by_xpath("/html/body/div/div[2]/div/div/div/from/span/input")

2)利用元素屬性定位

// 表示當前頁面某個目錄下,input 表示定位元素的標簽名,[@id='kw'] 表示這個元素的 id 屬性值等於 kw
如果不想指定標簽名也可以用星號( *)代替。當然,使用 XPath 不僅僅只局限在 idname class 這 三個屬性值,元素的任意屬性值都可以使用,只要它能唯一的標識一個元素。

例:

find_element_by_xpath("//input[@id='kw']")
find_element_by_xpath( "//input[@id='wd']")

find_element_by_xpath("//input[@class='s_ipt']")

find_element_by_xpath("//*[@class='bg s_btn']")

 3)層級與屬性結合:

例:browser.find_element_by_xpath("//*[@id='main']/div[1]/ul/li[2]/a").click()

4)使用邏輯運算符:

find_element_by_xpath( "//input[@id='kw' and @class='su']/span/input")

2.css定位:

1)通過class屬性定位:

find_element_by_css_selector() 方法用於 CSS 語言定位元素,點號(.)表示通過 class 屬性來定位元素。

如:browser.find_element_by_css_selector(".right_more")

2)通過 id 屬性定位:

井號(#)表示通過 id 屬性來定位元素。

例:find_element_by_css_selector("#kw")

3)通過標簽名定位:

CSS 語言中用標簽名定位元素不需要任何符號標識,直接使用標簽名即可,但我們前面已經了解到 標簽名重復的概率非常大,所以通過這種方式很難唯一的標識一個元素。

 4)通過父子關系定位:

find_element_by_css_selector("span>input")
5)通過屬性定位:

find_element_by_css_selector("input[autocomplete='off']")

find_element_by_css_selector("input[maxlength='100']")

find_element_by_css_selector("input[type='submit']")

CSS 當中也可以使用元素的任意屬性,只要這些屬性可以唯一的標識這個元素。

6)組合定位:

find_element_by_css_selector("span.bg s_ipt_wr>input.s_ipt")

find_element_by_css_selector("span.bg s_btn_wr>input#su")

有一個父元素,它的標簽名叫 span,它有一個 class 屬性值叫 bg s_ipt_wr,它有一個子元素,標簽名 叫 input,並且這個子元素的 class 屬性值叫 s_ipt

 

3.用 By 定位元素

find_element()方法只用於定位元素。它需要兩個參數,第一個參數是定位方式,這個由 By 提供;另 第二個參數是定位的值。在使用 By 時需要將 By 類導入。

from selenium.webdriver.common.by import By
例:
find_element(By.ID,"kw")

find_element(By.NAME,"wd")

find_element(By.CLASS_NAME,"s_ipt")

find_element(By.TAG_NAME,"input")

find_element(By.LINK_TEXT,u"新聞")

find_element(By.PARTIAL_LINK_TEXT,u"")

find_element(By.XPATH,"//*[@class='bg s_btn']")

find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")   

4.定位一組對象

 

定位一組對象的方法與定位單個對象的方法類似,唯一的區別是在單詞 element 后面多了一個 s 表示 復數。定位一組對象一般用於以下場景:

1)批量操作對象,比如將頁面上所有的復選框都被勾選。

2)先獲取一組對象,再在這組對象中過濾出需要具體定位的一些對象。比如定位出頁面上所有的 checkbox,然后選擇最后一個。
例:
checkboxs=driver.find_elements_by_xpath("//input[@type='checkbox']")
for checkbox in checkboxs:
checkbox.click()
pop() 函數用於獲取列表中的一個元素(默認為最后一個元素),並且返回該元素 的值。

driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()

pop() pop(-1)默認獲取一組元素中的最后一個
pop(0) 默認獲取一組元素中的第一個
pop(1)

 

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM