admin管理员组

文章数量:1327524

I'm having problems with html DOM.

How do I get the value from this path:

html body div table tbody tr td table tbody tr td table tbody tr td table tbody tr td form table tbody tr td

I can only find stuff like getElementbyID/tag/name/class etc.

How do I get the absolute DOM 'path' of the td element (let's say the 3rd cell of the second row in that table)? I've been looking everywhere but cannot find a simple answer without ID/Class etc involved.

I'm having problems with html DOM.

How do I get the value from this path:

html body div table tbody tr td table tbody tr td table tbody tr td table tbody tr td form table tbody tr td

I can only find stuff like getElementbyID/tag/name/class etc.

How do I get the absolute DOM 'path' of the td element (let's say the 3rd cell of the second row in that table)? I've been looking everywhere but cannot find a simple answer without ID/Class etc involved.

Share Improve this question edited Dec 15, 2012 at 20:07 Pranav 웃 8,4776 gold badges40 silver badges48 bronze badges asked Dec 15, 2012 at 19:38 Rpk_74Rpk_74 412 gold badges2 silver badges5 bronze badges 10
  • The reason i don't use ID's is because i'm writing a script for a asp page and the ID's are not static but change often, so that's the reason i can't use ID's – Rpk_74 Commented Dec 15, 2012 at 19:46
  • It's some browsergame and i want to automate certain stuff – Rpk_74 Commented Dec 15, 2012 at 19:47
  • 1 If you need such a path to find a specific element you must have done something wrong. Why is the selector so enormous? For example, why are there TR's in it? A TD is always wrapped by a TR as a TR is always wrapped by a table. You could shorten the selector to about three to four elements and achieve exactly the same. Also I don't understand whether you want to get an element for that selector or if you want to get such a selector for a specific element. – feeela Commented Dec 15, 2012 at 19:48
  • If the IDs are changing, use static classes on the columns instead. – Christian Commented Dec 15, 2012 at 19:50
  • 2 Using jQuery it's super simple: $('td:nth-child(6)', '#tblMarket') will return an array of all of the 6th TDs in the table. – Christian Commented Dec 15, 2012 at 19:53
 |  Show 5 more ments

4 Answers 4

Reset to default 3

You could use querySelector(), but it doesn't have great support...

var elem = document.querySelector('html body div table tbody tr td table tbody tr td table tbody tr td table tbody tr td form table tbody tr td');

Otherwise just use a library that allows you to use CSS selectors, such as jQuery.

var $elem = $('html body div table tbody tr td table tbody tr td table tbody tr td table tbody tr td form table tbody tr td');

By the way, selecting like this is horrible for performance. Absolutely terrible. Have a read up on CSS selectors to learn why.

First, consider whether you do really need full paths. Referring to IDs or classes is more robust as they have less moving parts.

If full paths are what you need, you may wish to use XPath, as it's specifically designed for finding elements by path.

Here's a simple cross browser XPath library - there are many others.

Looks like you may want something like this:

For the following sample HTML,

<html>
  <head>
    <script src="http://ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="findit.js"></script>
  </head>
  <body>
    <div id="header">
      <h1>Wele to my ASP site!</h1>
    </div>
    <div id="h440292">
      <table>
        <!-- tbody omitted, but some (all?) browsers add it -->
        <tr>
          <td>junk</td>
          <td>junk</td>
          <td>junk</td>
        </tr>
        <tr>
          <td>junk</td>
          <td>junk</td>
          <td>pick me!</td>
        </tr>
      </table>
    </div>
  </body>
</html>

this jQuery code will find the cell that says "pick me!"

$(function () {
    var $resultCell = $("body")
        .children("div").eq(1)
        .children("table")

        // Note I have to add this even though
        // I omitted the tbody in the HTML markup
        .children("tbody") 
        .children("tr").eq(1)
        .children("td").eq(2);
    alert($resultCell.text());
});

If performance bees a problem, you may need to resort to doing something similar using the native DOM methods.

I am playing with this idea. I want to be able to find any node on a page (using a headless browser). Trying to build an absolute node path I created a recursive function, works but I am finding it is not pletely unique which is annoying. On here for example, each post is templated so selecting text in the third response will reveal the same node path up the HTML tag as the first post

const buildPath = (node) => {
  console.log(node);
  if(node.tagName !== "HTML") {
    path.unshift(node.tagName.toLowerCase())
    buildPath(node.parentNode)
  }
};
const path = [];
builtPath(<start node>);
document.querySelector(path.join(" "))

but this is where I am stuck now. Some things don't have any specific classes or names or ids to add to that. I may need to capture the innertext or innerhtml and try to match that. Kinda annoying. I suppose you could load like D3 and inject an incremental ID as data but then the site can't change at all, which may be true for this method too but I would think less so.

本文标签: javascriptHow to get absolute DOM path of html elementStack Overflow