Selenium选择器小结
admin
2024-02-07 17:13:46
0

前言

最近在一些网上采集数据,目前大部分网站的数据都是动态获取的,例如最常见的通过下拉滚动条刷新列表数据。这就让传统的Scrapy爬虫工具无能为力了,虽然有Selemium, Playwright等插件对Scrapy的加持,但这些插件目前都不太完善,所以,我基本放弃了一切用Scrapy搞掂的想法。静态网站的采集使用Scrapy,动态网站则完全采用Selenium或puppeteer或Playwright。
经过对三者的试用,基本结论就是:

大部分动态数据的爬取采用Selenium的完全没问题的,Selenium对Python和其它语言的支持胜于puppeteer和Playwright,Selenium总体要成熟稳定一点。文档也更丰富。网上各种问题容易找到答案。

当然上述结论或许半年后就不成立了。因为后两者的发展很快。言归正传,掌握了Selenium的选择器,就掌握了Selenium的一半。尤其是CSS 选择器,简明好用,是首选。

几种典型的选择方式

ID选择

如果元素有ID,则优先采用ID定位。

XPath: //div[@id='example'] 
CSS: #example

根据Element类型选择

Xpath: //input
Css: =input

直接子元素

XPATH 采用斜杠 “/“定义, CSS选择器采用 “>”定义。

例子:

XPath: //div/a
CSS: div > a

非直接子元素

XPATH采用双斜杠 “//”,CSS采用空格。例子:

XPath: //div//a
CSS: div a

根据class类名选择

XPATH: “[@class=‘example’]”
CSS 选择器就是一个点号“.”

XPath: //div[@class='example']
CSS: .example

根据元素的文本选择

XPATH: //[ text() = ‘Get started free’ ]
XPATH: //
[ contains (text(), ‘Get started’ ) ]
CSS: <:><(text)>

CSS 选择器高级用法

Next Sibling 兄弟节点

This is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that’s inside the same parent. Let’s show an example using a form to select the field after username.

Login

Let’s write an XPath and css selector that will choose the input field after “username”. This will select the “alias” input, or will select a different element if the form is reordered.

XPATH: //input[@id=‘username’]/following-sibling:input[1]
CSS: #username + input
Attribute Values
If you don’t care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value. A good example would be choosing the ‘username’ element of the form above without adding a class.

We can easily select the username element without adding a class or an id to the element.

XPATH: //input[@name=‘username’]
CSS: input[name=‘username’]
We can even chain filters to be more specific with our selectors.

XPATH: //input[@name='login’and @type=‘submit’]
CSS: input[name=‘login’][type=‘submit’]
Here Selenium will act on the input field with name=“login” and type=“submit”

指定特殊匹配: nth-child 和 nth-of-type

CSS selectors in Selenium allow us to navigate lists with more finesse than the above methods. If we have a ul and we want to select its fourth li element without regard to any other elements, we should use nth-child or nth-of-type. Nth-child is a pseudo-class. In straight CSS, that allows you to override behavior of certain elements; we can also use it to select those elements.

  • Cat
  • Dog
  • Car
  • Goat

If we want to select the fourth li element (Goat) in this list, we can use the nth-of-type, which will find the fourth li in the list. Notice the two colons, a recent change to how CSS identifies pseudo-classes.

CSS: #recordlist li::nth-of-type(4)
On the other hand, if we want to get the fourth element only if it is a li element, we can use a filtered nth-child which will select (Car) in this case.

CSS: #recordlist li::nth-child(4)
Note, if you don’t specify a child type for nth-child it will allow you to select the fourth child without regard to type. This may be useful in testing css layout in selenium.

CSS: #recordlist *::nth-child(4)
In XPATH this would be similar to using [4].

子串匹配

CSS 选择器的一大特色就是字符串的匹配, 可以采用 ^=, $=, 或 *= 。

^= 匹配前缀
CSS: a[id^=‘id_prefix_’]
A link with an “id” that starts with the text “id_prefix_”

=匹配后缀CSS:a[id= 匹配后缀 CSS: a[id=匹配后缀CSS:a[id=‘_id_sufix’]
A link with an “id” that ends with the text “_id_sufix”

= 匹配子串
CSS: a[id
=‘id_pattern’]
A link with an “id” that contains the text “id_pattern”

总结

本文对selenium选择器的基本用法做了一一介绍,帮助大家掌握这一强大的自动化测试工具。当然,你搞自动化运维,做爬虫,没人拦着你。

相关内容

热门资讯

企业IP打造指南:小公司低成本... 小公司做企业IP,不是为了装门面,而是让客户在没见到你之前,就能通过内容知道你是谁、你解决什么问题、...
官方:赵心童入选世界斯诺克名人... 北京时间5月8日消息,世界斯诺克巡回赛(WST)今日正式公布了2025/26赛季年终奖项及名人堂更新...
小灰熊AI学员王锋:希望能跟上... 35了,老程序员了。 从进入互联网行业到现在,其实已经做了很多年移动端开发。最早那几年,安卓行业发展...
原创 2... 2026年全国两会把稳定房地产市场列为重点工作,政府工作报告明确提出因城施策控增量、去库存、优供给。...
一年翻倍,六年未归——徽商银行... 文:向善财经 今年的港股市场,与A股市场出现了明显的分化。 A股这边,科技板块在AI浪潮中热闹非凡;...
古井贡酒2025:在行业深度调... 以“稳”为底、以“新”为翼。 文/每日财报 杜康 在行业库存高企、价格倒挂的背景下,当多数酒企在为...
好上好8408万收购鼎瑞芯加码... 5月7日晚,好上好(001298.SZ)抛出一份收购公告,拟以8408万元现金收购深圳市鼎瑞芯科技有...
全面大撤离!李嘉诚英国“套现”... 突发,李嘉诚又卖了。 这次,套现了455亿。 金额不少,但更值得关注的是透露着不同寻常的信号。 因为...
油气价格上涨加剧法国一季度贸易... 据新华社,法国海关7日发布的数据显示,受中东局势推高国际油气价格影响,法国今年第一季度贸易逆差扩大至...
昆仑芯启动科创板IPO上市辅导... 5月8日,据证监会官网显示,昆仑芯(北京)科技股份有限公司于2026年5月7日正式启动科创板上市辅导...
贵州茅台酒股份有限公司关于回购... 来源:上海证券报 证券代码:600519 证券简称:贵州茅台 公告编号:临2026-016 贵州茅...
百度昆仑芯启动科创板上市辅导,... 5月8日,证监会官网显示,昆仑芯(北京)科技股份有限公司 (下称“昆仑芯”)于2026年5月7日正式...
滕州信华的承压时刻:罚单、失信... 2026年4月末,滕州信华美元债单日跌近2%,关联方被列“老赖”。半年前,这家AA+城投曾因非市场化...
002808,或被终止上市! 【导读】因触及财务类退市指标,*ST恒久或被终止上市 中国基金报记者 李智 又一A股或被终止上市。 ...
院士团队掌舵,溧阳这家企业已完... 近日,溧阳天目先导电池材料科技有限公司(下称“天目先导”)官宣完成B轮融资,投资方包括知卓创新资本、...
工商银行全新推出“工盈研选”品... 深圳商报·读创客户端记者 詹钰叶 近日,工商银行重磅推出「工盈研选」基金销售服务品牌,以客户盈利为核...
和讯信息胡云龙:逼空走势,周五... 今天市场出现逼空走势,场内投资者因持有筹码而尤为受益。五一前布局的投资者当前收获颇丰。然而,随着上证...
今晚,油价上调! 4月21日国内成品油价格下调以来,国际市场原油价格剧烈震荡,前期大幅上涨后近日有所回落,本次调价的前...
南方东英旗下两倍做多海力士,成... 【导读】南方东英旗下两倍做多海力士,成为全球最大的个股杠杆及反向产品 中国基金报记者 伊万 人工智能...
原创 金... 黄金,这东西从古至今就没离开过中国人的生活。从老辈人压箱底的小黄鱼,到如今年轻人结婚绕不开的“三金”...