4

I need to perform data validation of two tables using Selenium.

Given a properly marked-up HTML table filled with data:

<table>
    <tbody>
        <tr>
            <td>A</td>
            <td>B</td>
            <td>C</td>
        </tr>

        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
    </tbody>
</table>

And I want to "deserialize" this table (gather its data) into a bi-dimensional array (String[][]) using Selenium. The reason I want to do so is that I have another HTML table (on the other web-page) that contains supposedly the same data stored in it - and I need to perform data validation between those two tables.

I have tried lots of options on how to solve this problem, and iterative cell-by-cell data gathering (locating cells either using the getTable() or getText() methods) is not one of them - since it takes enormous amounts of time to complete a big table on an overloaded web-page.

JavaScript injection (using the getEval() method) is not available in my case since the table resides in an <iframe> that has an origin (base URL) that differs from the one of the main page. And according to same origin policy this cannot be performed.

Guys, any idea on how to solve the given problem?

John Doe
  • 4,574
  • 2
  • 26
  • 30

1 Answers1

1

You could use JAXB to deserialize the HTML text into plain java object hierarchy and then construct a 2D array from those objects.

Another option: parse the text as XML into a org.w3c.dom.Document and use XPath in Java to find and iterate over the elements.

Andrejs
  • 26,885
  • 12
  • 107
  • 96
  • Are you implying to use the **`getHtmlSource`** method? Are there any Selenium alternatives to get just a part of HTML instead of the whole? – John Doe Apr 05 '12 at 13:36
  • 1
    Either using getHtmlSource/getBodySource and then finding the element or element.getAttribute("innerHTML") – Andrejs Apr 05 '12 at 14:43
  • Isn't **`innerHTML`** an object property other than an attribute? – John Doe Apr 05 '12 at 15:03
  • 1
    It tries to get the attribute first, if that does not exist it looks for a property – Andrejs Apr 05 '12 at 16:23