惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
A
Arctic Wolf
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
I
Intezer
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
Google DeepMind News
Google DeepMind News
月光博客
月光博客
Attack and Defense Labs
Attack and Defense Labs
Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
量子位
IT之家
IT之家
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
腾讯CDC
T
Troy Hunt's Blog
SecWiki News
SecWiki News
罗磊的独立博客
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
阮一峰的网络日志
阮一峰的网络日志
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Last Watchdog
The Last Watchdog
美团技术团队
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Last Week in AI
Last Week in AI
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
O
OpenAI News
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
P
Privacy International News Feed
TaoSecurity Blog
TaoSecurity Blog

🛫Qifei's Blog

Python的模块构建和调用方式 - 🛫Qifei's Blog 如何选择搭建梯子用的VPS? - 🛫Qifei's Blog Hexo博客页面引入足迹地图 - 🛫Qifei's Blog 机器学习和scikit-learn库基础学习笔记 - 🛫Qifei's Blog 利用国内服务器加速v2ray访问速度和规避检查 - 🛫Qifei's Blog Matplotlib_Bar3d绘制彩色带颜色标尺的3D柱形图 - 🛫Qifei's Blog Linux后台运行nostr虚荣公钥的挖掘 - 🛫Qifei's Blog Hexo博客添加Nostr_NIP-05认证 - 🛫Qifei's Blog Nostr_NIP-05认证服务简介和配置 - 🛫Qifei's Blog Onedrive_API大文件上传记录 - 🛫Qifei's Blog 反爬虫-如何检测有没有使用Puppeteer - 🛫Qifei's Blog Vuejs学习笔记 - 🛫Qifei's Blog OpenOffice连接Mysql数据库 - 🛫Qifei's Blog 安卓ToyVPN服务端从零开始读 - 🛫Qifei's Blog RWTH自习室自动预定程序(RWTH_Lernraum) - 🛫Qifei's Blog RWTH自习室自动预定程序(RWTH_Lernraum) - 🛫Qifei's Blog 如何利用Selenium实现更加高效的爬虫 - 🛫Qifei's Blog Python如何准确的计算Http请求中的Content_Length - 🛫Qifei's Blog 破解图片等资源跨域和防盗链阻拦 - 🛫Qifei's Blog Selenium-Webdriver接口 - 🛫Qifei's Blog
在控制器中用JS验证CSS_Selector和Xpath - 🛫Qifei's Blog
Qifei · 2021-07-25 · via 🛫Qifei's Blog

0:引言

使用Beautifulsoup和Selenuim等网页分析工具时,经常需要自己构造一个选择器,最常用的是CSS选择器和Xpath。如何验证选择器构造是否正确呢?我们可以直接在网页的开发者工具Console栏里使用JS代码来验证选择器的正确性。

1:CSS Selector验证

1
2
css_selector = '#bs_form_main input[value=buchen]'
document.querySelector(css_selector)

JS有提供querySelector方法,如果返回值不为Null,则说明选择器构造正确。

2:Xpath验证

1
2
xpath='//*[@id="bs_tr3FE6EE511403"]/td[9]/input'
document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null).iterateNext()

evaluate方法返回XpathResult对象,通过iterateNext可以依次获取符合选择的Dom。通常情况下,不需要确认是否有多个符合条件的返回值,所以如果返回不是null,则xpath构造正确

如果需要确认,返回一个或者多个符合xpath的dom,可以使用下面的方法。

1
2
3
4
5
6
7
8
function x(xpath) {
var result = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
var i = 0;
while(result.iterateNext()){
i++;
}
return i;
}

3:参考

JavaScript 技术篇-js通过xpath获取dom节点,js校验xpath唯一性。