python-xpath定位方法詳解

來源:互聯網
上載者:User

標籤:技術分享   str   lin   ext   python   nbsp   find   contains   span   

一、xpath基本定位用法

  1.1 使用id定位 -- driver.find_element_by_xpath(‘//input[@id="kw"]‘)

  

  1.2 使用class定位 -- driver.find_element_by_xpath(‘//input[@class="s_ipt"]‘)

  

  1.3 當然 通過常用的8種方式結合xpath均可以定位(name、tag_name、link_text、partial_link_text)以上只列舉了2種常用方式哦。

二、xpath相對路徑/絕對路徑定位

  2.1 相對定位 -- 以// 開頭 如://form//input[@name="phone"]

  

  2.2 絕對位置 -- 以/ 開頭,但是要從根目錄開始,比較繁瑣,一般不建議使用 如:/html/body/div/a

  

三、xpath文本、模糊、邏輯定位

  3.1【文本定位】使用text()元素的text內容 如://button[text()="登入"]

  

  3.2 【模糊定位】使用contains() 包含函數 如://button[contains(text(),"登入")]、//button[contains(@class,"btn")]

  

  3.3 【模糊定位】使用starts-with -- 匹配以xx開頭的屬性值;ends-with -- 匹配以xx結尾的屬性值  如://button[starts-with(@class,"btn")]、//input[ends-with(@class,"-special")]

  3.4  使用邏輯運算子 -- and、or;如://input[@name="phone" and @datatype="m"]

四、xpath軸定位

  4.1  軸運算

  ancestor:祖先節點 包括父
  parent:父節點
  prceding-sibling:當前元素節點標籤之前的所有兄弟節點
  prceding:當前元素節點標籤之前的所有節點   following-sibling:當前元素節點標籤之後的所有兄弟節點  following:當前元素節點標籤之後的所有節點    使用文法: 軸名稱 :: 節點名稱  使用較多情境:頁面顯示為一個表格樣式的資料列  如:  

  

注意:

#定位 找到元素 -- 做到唯一識別
#優先使用id
#捨棄:有下標的出現、有絕對位置的出現、id動態變化時捨棄

 

 

 

python-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.