python css selector_selenium+python find_element_by_css_selector方法使用

1.通过类class获取

比如如下代码

This heading is very important.

This paragraph is very important.

This paragraph is a very important warning.

在上面的代码中,两个元素的 class 都指定为 important:第一个标题( h1 元素),第二个段落(p 元素)

1> 获取class值为important的h1标签

find_element_by_css_selector(h1.importane)

2>获取所有class值为important的标签

find_element_by_css_selector(*.importane)或者find_element_by_css_selector(.importane)

3>获取class值为important warning的标签

find_element_by_css_selector(.importane.warning)

2.通过id获取:

首先,ID 选择器前面有一个 # 号 - 也称为棋盘号或井号

This is a paragraph of introduction.

find_element_by_css_selector(#"intro")

3.属性选择器:

1>.

W3School

属性中包含了title和href,

find_element_by_css_selector('a[title][href]')

2>

About W3School

定位属性中href="http://www.w3school.com.cn/about_us.asp"的元素,

find_element_by_css_selector('a[href="http://www.w3school.com.cn/about_us.asp"]')

3>

W3School

通过href和title来定位

find_element_by_css_selector("a[href='http://www.w3school.com.cn/about_us.asp'][title='W3School']")

4>部分属性定位

可以应用样式:

figure-1.gif

figure-2.gif


无法应用样式:

figure-1.gif

figure-2.gif

定位title中包含有figure的元素:

find_element_by_css_selector("image[title~='figure']")

5>其他

[abc^="def"] 选择 abc 属性值以 "def" 开头的所有元素

[abc$="def"] 选择 abc 属性值以 "def" 结尾的所有元素

[abc*="def"] 选择 abc 属性值中包含子串 "def" 的所有元素

4.后代选择器

This is a important heading

This is a important paragraph.

find_element_by_css_selector("h1 em")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值