首页 > 编程语言 > 详细

python slenium 中CSS定位

时间:2019-01-16 12:53:45      阅读:288      评论:0      收藏:0      [点我收藏+]

以百度为例

一、通过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下的第二个元素

python slenium 中CSS定位

原文:https://www.cnblogs.com/gxfaxe/p/10276322.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!