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选择器的基本用法做了一一介绍,帮助大家掌握这一强大的自动化测试工具。当然,你搞自动化运维,做爬虫,没人拦着你。

相关内容

热门资讯

消息称百度旗下昆仑芯瞄准500... 6 月 29 日消息,据《The Information》昨日援引知情人士消息,百度旗下 AI 芯片...
打造夏日消费新场景 第35届北... 北京商报讯(记者 翟枫瑞)6月29日消息,第35届北京国际燕京啤酒文化节新闻发布会在京举行。本届啤酒...
社保基金持仓数据出炉,一季度增... 最近各大上市公司一季度财报都公开了,咱们国家社保基金的持仓数据也全部曝光。目前社保拿着比亚迪价值44...
36氪首发 | 海思、中兴团队... 作者 | 乔钰杰 编辑 | 袁斯来 硬氪获悉,广州宸思通讯科技有限公司(以下简称“宸思科技”)近日完...
两天蒸发47亿市值!一纸税务通... 一纸税务通知书,能让一家百亿龙头两天蒸发47亿市值。 6月22日,北大荒(600598.SH)公告称...
SK海力士将投资1100万亿韩... SK集团会长崔泰源6月29日在韩国“三大重大计划”发布会上宣布,公司将投资1100万亿韩元扩大半导体...
两只A股,终止上市! 两家A股公司,即将摘牌。 6月29日,退市沪科(600608.SH)公告称,上海证券交易所将在202...
原创 M... 一家成立近十年的自动驾驶公司,在IPO时吸引了14家基石投资者认购近一半的发行股份,其中不乏奔驰、比...
基金忠言|国寿安保滤镜碎,三年... 图片来源:视觉中国 蓝鲸新闻6月29日讯(记者 祁和忠)保险系基金公司国寿安保总经理换人了。 6月2...
三星电机计划加码玻璃基板!相关... 6月29日,玻璃基板概念股午后有所回升, 华工科技(000988.SZ)逼近涨停, 彩虹股份(600...
拉萨海关持续壮大外贸经营主体 ...   新华网拉萨6月28日电(记者蒋梦辰)近日,记者从拉萨海关获悉,今年前5个月,西藏有进出口实绩的外...
机构:二季报临近,医药生物板块... 6月29日,华源证券发布了一篇医药生物行业的研究报告,报告指出,业绩期临近,产业链景气度有望再次迎来...
每日收评科创50放量涨超4.5... 财联社6月29日讯,三大指数全线收红,创业板指探底回升,科创50指数大涨4.61%。沪深两市成交额3...
6月多地土拍结构性升温:深圳单... 进入2026年6月,不少城市核心区地块集中诞生高溢价宗地,热度突出的城市包含深圳、杭州、长沙。 其中...
业绩炸裂!盛达资源半年预盈3.... 6月29日,贵金属矿山龙头盛达资源(000603.SZ)发布 2026 年半年度业绩预告,上半年业绩...
A股午后拉升三大股指收涨:半导... A股三大股指6月29日开盘涨跌互现。早盘沪强深弱,创指一度跌超2%。半导体午后拉升,带动两市上涨,沪...
原创 空... 前言 大家好,我是老金。 这几天,两幅极度割裂的画面放在一起,把我看笑了。 一边是在持续的热浪下,欧...
澳大利亚审慎监管局拟放宽银行风... 澳大利亚审慎监管局(APRA)6月29日就修改 银行信用风险资本设定公开征求意见,旨在加大信贷投放以...
全民炒股,急踩刹车!韩国股市突... 屈红燕/证券时报网 全民狂欢、交易高度拥挤、杠杆资金猛增、新入市投资者表现激进、大型IPO吸金等现象...