selenium之CSS定位匯總


以百度首頁為例:

定位輸入框

一:單一屬性定位

1:type selector

driver.find_element_by_css_selector('input')

2:id 定位

driver.find_element_by_css_selector('#kw')

3:class 定位

driver.find_element_by_css_selector('.s_ipt')

4:其他屬性定位

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

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

二:組合屬性定位

1:id組合屬性定位

driver.find_element_by_css_selector("input#kw")

2:class組合屬性定位

driver.find_element_by_css_selector("input.s_ipt")

3:其他屬性組合定位

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

4:僅有屬性名,沒有值也可以

driver.find_element_by_css_selector("input[name]")

5:兩個其他屬性組合定位

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

6:模糊匹配屬性值方法

以百度首頁點擊按鈕為例

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,唯一的標簽

 


免責聲明!

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



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