Search code examples
javascriptautomationcrmimacros

Getting inner HTML of a <td> after previous <td>


So, I'm working in possibly the most user-unfriendly CRM and I wanted to make my life a little bit easier with iMacros.

However, things are so complicated that I can't do a simple task of getting some value easily.

Here's how stuff looks like on the page:

<td id="_t5141276" class="Label">Number:</td>
<td id="_t5141277" class="Value">1234567890</td>

I need to get innerhtml of "Value" class, BUT:
1) There's bunch of Values on the page
2) IDs are generated randomly for every page.

I figured out that I could look for the "Number:" text in first td, and then get innerhtml of the td after that one, but how do I do it?

I'd prefer it to be in javascript or something else that I could easily integrate into iMacros.


Solution

  • I'm going to assume you can't change the markup in any way, as that seems to be implied in your question.

    You can get a list of all td elements in document order from querySelectorAll. When you find the one with Number: in it, just use the next one:

    var list = document.querySelectorAll("td"); // See note below
    var index;
    var value;
    for (index = 0; index < list.length - 1; ++index) {
        if (list[index].innerHTML === "Number:") {
            value = list[index + 1].innerHTML;
            break;
        }
    }
    

    Note I've allowed for the possibility that the Number: item is the last one (and so the next, the one we want, would be missing) by stopping one short of the last one.


    Note: The above does it in a webpage in the normal way. To do it in iMacros, apparently you have to prepend window.content to document.querySelectorAll, so the first line would be:

    var list = window.content.document.querySelectorAll("td");
    //         ^^^^^^^^^^^^^^^------- added for iMacros