selenimu--find_element_by_css_selector()方法匯總


一、單一屬性定位

  • type selector

  driver.find_element_by_css_selector('input') 

  • id 定位

  driver.find_element_by_css_selector('#kw')

  • class定位

  driver.find_element_by_css_selector('.s_ipt')

  • 其他屬性定位

  driver.find_element_by_css_selector('[name='wd']')

  driver.find_element_by_css_selector([type='text'])

二、組合屬性定位

  • id組合屬性定位

  driver.find_element_by_css_selector("input#kw")

  • class組合屬性定位

  driver.find_element_by_css_selector("input.s_ipt")

  • 其他屬性組合定位

  driver.find_element_by_css_selector("input[name='wd']")

  • 僅有屬性名,沒值也可以

  driver.find_element_by_css_selector("input[name]")

  • 兩個其他屬性組合定位

  driver.find_element_by_css_selector("[name='wd'][autocomplete='off']")

  • 模糊匹配屬性值方法

  以百度首頁點擊按鈕為例

   

  1>屬性值由多個空格隔開,匹配其中一個值的方法

  driver.find_element_by_css_selector("input[class~='btn']")

  2>匹配屬性值為字符串開頭的方法

  driver.find_element_by_css_selector("input[class^='btn']")

  3>匹配屬性值字符串結尾的方法

  driver.find_element_by_css_selector("input[class$='s_btn']")

  

 

   

  4>匹配被-分割的屬性值的方法,如上圖的class

  driver.find_element_by_css_selector("input[class|='s']")  #要求精確填寫的屬性值

三、層次定位

1:E>F    E下面的F這個元素

driver.find_element_by_css_selector('from#form>span>input')#id是form的form下面的span下面的input

2:E:nth-child(n)  如上圖,

driver.find_element_by_css_selector('#u_sp > a:nth-child(1)')#id為u_sp的下面的第一個a標簽。

3:E:nth-last-child(n),如字面意思:倒數第幾個標簽

4:E:first-child,第一個標簽

5:E:last-child,最后一個標簽

6:E:only-child,唯一的標簽

 

參考博客:https://www.cnblogs.com/haifeima/p/10138154.html


免責聲明!

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



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