简体   繁体   English

如果没有 ID,则查找特定的 html 元素

[英]Find specific html element if it has no ID

I currently have an application where users can highlight a specific html element on any website.我目前有一个应用程序,用户可以在其中突出显示任何网站上的特定 html 元素。 User A can share this element with the other user, where user B will open the site and see that element highlighted.用户 A 可以与其他用户共享此元素,用户 B 将打开该站点并看到该元素突出显示。 The problem is how to find the element that has been highlighted on the site if it has no ID.问题是如果没有 ID,如何找到在站点上突出显示的元素。 I can copy its outerHTML and then find element by that, but I am not sure that will always work, what if a few elements have the same outerHTML.我可以复制它的outerHTML,然后通过它找到元素,但我不确定它是否总是有效,如果一些元素具有相同的outerHTML怎么办。

I have the feeling that you are looking for XPath .我感觉您正在寻找XPath XPath is basically the unique path to your DOM element. XPath 基本上是您的 DOM 元素的唯一路径。 You can read more about it here .你可以在这里阅读更多关于它的信息。

Examples of XPaths: XPath 示例:

//*[@id="question"]
/html/body
/html/body/div[3]/div[2]/div/div[1]/div[3]/div[1]/div/div[3]

You can get the XPath on any webpage by opening up the Inspector (in the developer console), right clicking the desired element, choosing copy and then XPath from the dropdown menu.您可以通过打开检查器(在开发人员控制台中),右键单击所需元素,选择复制,然后从下拉菜单中选择 XPath,在任何网页上获取 XPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM