python slenium 中CSS定位
以百度為例
一、通過id、class定位
1、#id:python:driver.find_element_by_css_selector('input#kw')
2、.class:python:driver.find_element_by_css_selector('input.s_ipt')
注:class='bg s_ipt_wr quickdelete-wrap',類似這種叫復合class,由多個類選擇器組成,定位的寫法則是:.bg.s_ipt_wr.quickdelete-wrap,所有空格用.(點)代替
二、其他屬性
使用其他屬性定位,如name,type。。。
python:driver.find_element_by_css_selector('input[name="wd"]')
如果屬性沒有值,也可以使用
python:driver.find_element_by_css_selector('input[name]')
三、模糊匹配,定位百度輸入框
匹配屬性值由多個空格隔開,匹配其中一個值:input[class~='bg']? # 百度搜索按鈕:class='bg s_btn'
匹配屬性值為 'bg' 開頭的方法:python:driver.find_element_by_css_selector("input[class^='bg']")
匹配屬性值 ‘ipt’ 結尾的方法:python:driver.find_element_by_css_selector("input[class$='ipt']")
匹配屬性值中間有 '_i' 的元素:python:driver.find_element_by_css_selector("input[class*='_i']")
四、組合屬性定位
1、input#id.class[name='wd']
2、多個屬性組合input[name][autocomplete]
五、層級定位
1、子元素定位(以 > 分隔)
form>span#id>input[name]
2、后代元素定位(以空格分隔)
span input :定位span下所有的input標簽,包括span下的所有層次的input,注意不是span下一層的input,是所有
3、元素層級css還支持三個方法,分別是first-child、last-child、nth-child(n)
(1)first-child:第一個后代元素
(2)last-child:最后一個后代元素
(3)nth-child(N):指定第N個后代元素
下面舉例:
(1)input:first-child ? 定位所有層次第一個為input的元素,注意是第一個元素為input標簽的
(2)span?input:first-child ? ?定位span標簽下,第一個為input標簽的元素
(3)span :last-child ? 定位span標簽下,最后一個元素
(4)span input:last-child ? ?定位span標簽下,最后一個為input標簽的元素
(5)span :nth-child(2) ?定位span標簽下,第二個元素
(6)form.fm>:nth-child(2) ?定位form標簽,class等于fm下的第二個元素
轉載于:https://www.cnblogs.com/gxfaxe/p/10276322.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的python slenium 中CSS定位的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019-1-15
- 下一篇: Python爬虫入门教程石家庄链家租房数