使用Chrome DevTools(console ande elements panel)进行xpath/css/js定位
Google Chrome provides a built-in debugging tool called "Chrome DevTools" out of the box, which includes a handy feature that can evaluate or validate XPath/CSS selectors without any third party extensions.
This can be done by two approaches:
- Use the search function inside
Elements
panel to evaluate XPath/CSS selectors and highlight matching nodes in the DOM. - Execute tokens
$x("some_xpath")
or$$("css-selectors")
inConsole
panel, which will both evaluate and validate.
From Elements panel
- Press
F12
to open up Chrome DevTools. Elements
panel should be opened by default.- Press
Ctrl
+F
to enable DOM searching in the panel. - Type in XPath or CSS selectors to evaluate.
- If there are matched elements, they will be highlighted in DOM.
However, if there are matching strings inside DOM, they will be considered as valid results as well. For example, CSS selectorheader
should match everything (inline CSS, scripts etc.) that contains the wordheader
, instead of match only elements.
From Console panel
- Press
F12
to open up Chrome DevTools. - Switch to
Console
panel. - Type in XPath like
$x(".//header")
to evaluate and validate. - Type in CSS selectors like
$$("header")
to evaluate and validate. - Check results returned from console execution.
- If elements are matched, they will be returned in a list. Otherwise an empty list
[ ]
is shown.
- If elements are matched, they will be returned in a list. Otherwise an empty list
1 2 3 4 5 | $x( ".//article" ) [<article class = "unit-article layout-post" >…</article>] $x( ".//not-a-tag" ) [ ] |
- If the XPath or CSS selector is invalid, an exception will be shown in red text. For example:
1 2 3 4 5 | $x( ".//header/" ) SyntaxError: Failed to execute 'evaluate' on 'Document' : The string './/header/' is not a valid XPath expression. $$( "header[id=]" ) SyntaxError: Failed to execute 'querySelectorAll' on 'Document' : 'header[id=]' is not a valid selector. |
js定位
console里面执行javascript代码,操作dom对象。
每个载入浏览器的 HTML 文档都会成为 Document 对象。Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。
通过id获取
document.getElementById(“id”)
通过name获取
document.getElementsByName(“Name”) 返回的是list
通过标签名选取元素
document.getElementsByTagName(“tag”)
通过CLASS类选取元素
document.getElementsByClassName(“class”)
兼容性:IE8及其以下版本的浏览器未实现getElementsByClassName方法
通过CSS选择器选取元素
document.querySelectorAll(“css selector")
兼容性:IE8及其以下版本的浏览器只支持CSS2标准的选择器语法
转自:
https://yizeng.me/2014/03/23/evaluate-and-validate-xpath-css-selectors-in-chrome-developer-tools/
https://blog.csdn.net/jamieblue1/article/details/101075193
作者:Glen.He
出处:http://www.cnblogs.com/puresoul/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架