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

相关内容

热门资讯

罕见9连跌,历史第二次!上证5... 最近,A股市场“冰火两重天”,有色金属、国防军工、电子等行业轮番走强,而大盘蓝筹却持续阴跌。 截至1...
见证历史!纽约白银期货、伦敦现... 1月23日晚,又见证历史了。1月23日晚间,纽约白银期货、伦敦现货白银双双突破100美元历史性关口!...
原创 淘... 当电商转向价值竞争,好服务成了好增长的密钥。 原创ⓒ新熵 新消费组 作者丨栀子 编辑丨九黎 进入20...
晚上9点后别做这几件事!一个好... 太原龙城中医医院科普:对于肺结节人群而言,除了饮食、运动等日常养护,优质睡眠的重要性往往被忽视。中医...
破“7”!人民币汇率中间价调升... 北京商报讯(记者 廖蒙)1月23日,中国人民银行授权中国外汇交易中心公布,当日银行间外汇市场人民币汇...
原创 异... 在探讨异性交往的微妙关系时,我们不得不提到一个至关重要的话题——男性回家后的行为模式。高情商的女性往...
和讯投顾王海洋:大盘震荡收星,... 1月23日,和讯投顾王海洋表示,大盘震荡收星,平均股价再创新高。昨日曾提到,今日大盘大概率会再次触摸...
马斯克成为科技行业“风向标” ... 来源:@投中网微博 【马斯克成为科技行业“风向标” 带火汽车航天光伏三大领域】作为科技行业标杆与全球...
凝“新”聚力│发挥“联”优势 ... 前 言 为深入贯彻落实习近平总书记同全国妇联新一届领导班子成员集体谈话时提出的“加大在新经济组织、新...
【日常消费品ETF收涨约0.8... 【日常消费品ETF收涨约0.8%,领跑美股行业ETF,半导体ETF跌约0.7%,银行业ETF跌超3....
每月最高800元!中度以上失能... 民政部、财政部日前印发通知,从今年1月1日起,面向中度以上失能老年人发放养老服务消费补贴的政策在全国...
龙虎榜揭秘!大牛股背后资金动向... 龙虎榜揭秘。 近期A股市场整体波动较为平稳,但不少个股波动剧烈,甚至连续涨停或连续跌停,近日的龙虎榜...
原创 全... 全球都在用美元? 中国偷偷搞了个大动作! 美元占全球支付50%时人民币干了啥? 你可能不知道的是,当...
我国银行理财市场规模突破33万... 银行业理财登记托管中心1月23日发布的《中国银行业理财市场年度报告(2025年)》显示,截至2025...
最高分红率35%!上市银行春节... 随着春节临近,上市银行2025年中期分红逐渐进入尾声。 1月23日,华夏银行、渝农商行迎来2025年...
蔡英丽医生:帕金森患者麻醉注意... 帕金森病是中老年人常见的神经系统退行性疾病,随着病情进展,不少患者可能需要接受各类手术,而麻醉环节的...
原创 利... 朋友们,今天A股发生了一件挺有意思的事:在地面光伏行业不少公司还在为亏损发愁的时候,一个叫做“太空光...
二游王战之局,鹰角先下一城 2026二游王战的启幕来了。1月22日,鹰角《明日方舟:终末地》(以下简称终末地)正式公测,和我们预...
大润发首次跨界合作蛋仔派对,以... 2026年春节前夕,高鑫零售旗下核心品牌大润发与国民级游戏IP《蛋仔派对》正式达成深度跨界合作,共同...
2025年基金4季报重仓股全扫... 随着基金2025年4季报基本披露完毕,记者注意到,截至去年底,基金的重仓股发生了比较明显的变化,有5...