首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用XPath获取Image属性值

如何使用XPath获取Image属性值
EN

Stack Overflow用户
提问于 2022-01-23 06:28:17
回答 1查看 583关注 0票数 0

我想使用XPath从标记中提取图像url,但唯一的问题是它使用下面的XPath返回整个src。

用于Chrome检查工具的查询:

代码语言:javascript
复制
$x("//div[@class='specs-photo-main']//img/@src")

输出:

代码语言:javascript
复制
[src]
0: src
baseURI: "https://www.gsmarena.com/xiaomi_11i_hypercharge-11186.php"
childNodes: NodeList []
firstChild: null
isConnected: false
lastChild: null
localName: "src"
name: "src"
namespaceURI: null
nextSibling: null
nodeName: "src"
nodeType: 2
nodeValue: "https://fdn2.gsmarena.com/vv/bigpic/xiaomi-11i-hypercharge.jpg"
ownerDocument: document
ownerElement: img
parentElement: null
parentNode: null
prefix: null
previousSibling: null
specified: true
textContent: "https://fdn2.gsmarena.com/vv/bigpic/xiaomi-11i-hypercharge.jpg"
value: "https://fdn2.gsmarena.com/vv/bigpic/xiaomi-11i-hypercharge.jpg"
[[Prototype]]: Attr
length: 1
[[Prototype]]: Array(0)

有办法获取"src“属性的"nodeValue”吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-23 19:10:41

您需要访问第一个元素(就像您需要找到第一个节点一样),然后您可以获得找到节点的属性:

代码语言:javascript
复制
$x("//div[@class='specs-photo-main']//img/@src")[0].nodeValue

或,

代码语言:javascript
复制
$x("//div[@class='specs-photo-main']//img/@src")[0].textContent

如果您需要获得纯文本值。

另见nodeValue vs innerHTML and textContent. How to choose?innerText vs innerHTML vs label vs text vs textContent vs outerText

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70819687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档