0

Here is the html I am working on, the concept is to loop through the table and fetch all the td values. And based on the first td value, fetch the second td with tag strong inside the td tag e.g: I have to fetch Case ID so i need to first check the first td value, if it is Case id than based on the i need to save the value inside the strong tag in a variable and use this value in the code

<table class="table table-condensed">
        <tbody><tr>
            <td>Case ID: </td>
            <td> <strong>3406872</strong></td>
        </tr>
                                    <tr>
                <td>Date of Case: </td>
                <td><strong>24-09-2018 05:54 AM</strong></td>
            </tr>
                                    <tr>
                <td>Name: </td>
                <td><strong> </strong></td>
            </tr>
                                                    <tr>
                <td>Email: </td>
                <td><strong><a href="mailto:"></a></strong></td>
            </tr>
            <tr>
                <td>Phone:</td>
                <td><strong></strong></td>
            </tr>
            <tr>
                <td>Phone Mobile:</td>
                <td><strong></strong></td>
            </tr>
            <tr>
                <td>Address: </td>
                <td>
                    <strong><br>  </strong>
                </td>
            </tr>

                                 <tr>
                     <td>Store Of Purchase: </td>
                    <td><strong>JBNZ RICCARTON (029)</strong></td>
                 </tr>

                            <tr>
                <td>Purchase Date: </td>
                <td><strong>10-02-2017</strong></td>
            </tr>
                                                    <tr>
                <td>Purchase Price: </td>
                <td><strong>$463.00</strong></td>
            </tr>
                    <tr>
            <td>Receipt/Tax invoice Number:</td>
            <td><strong>0307618717 41258 029 17</strong></td>
        </tr>
                                            <tr>
            <td>Floor Stock:</td>
            <td><strong>No</strong></td>
        </tr>
                    <tr>
            <td>Repaired Previously: </td>
            <td><strong>No</strong></td>
        </tr>
                                <tr>
            <td>Abuse/Misuse:</td>
            <td><strong>No</strong></td>
        </tr>
                    <tr>
            <td>Marked/Scratched: </td>
            <td><strong>No</strong></td>
        </tr>
        <tr>
            <td>Marked/Scratched Comment: </td>
            <td><strong></strong></td>
        </tr>
                    <tr>
            <td>Fault Report:</td>
            <td><strong>Product gives electric shock to user while in use.</strong></td>
        </tr>
                    <tr>
            <td>Fault Code:</td>
            <td><strong>Other (I)</strong></td>
        </tr>

                    <tr>
            <td>Retailer can Replicate: </td>
            <td><strong>No</strong></td>
        </tr>
        <tr>
            <td>If not, why not: </td>
            <td><strong></strong></td>
        </tr>
                    <tr>
            <td>Within automatic replacement period:</td>
            <td>
                <strong>
                    No                    </strong>
            </td>
        </tr>
                        <tr>
                <td> Within warranty period:</td>
                <td><strong>No</strong></td>
            </tr>
                            <tr>
                <td>Within assess for repair period:</td>
                <td><strong>No</strong></td>
            </tr>
                                        <tr>
            <td>Liability of repair:</td>
                            <td><strong>Vendor</strong></td>
        </tr>


                        <tr>
               <td>Disclaimer:</td>
               <td><strong>No</strong></td>
            </tr>
                                         <tr>
        <td>Case Type:</td>
        <td><strong>Requested in store</strong></td>
    </tr>

                                        <tr>
            <td>Case Status:</td>
            <td class="">
            <strong> Closed, replaced                                </strong>
            </td>
        </tr>

The Code that i am writing is:

List<WebElement> allrows= driver.findElements(By.xpath("//table[@class='table table-condensed']/tbody/tr"));
    int rows = allrows.size();
    //System.out.println(rows);
    for(int i=0; i<rows;i++) {
        List<WebElement> Columns= allrows.get(i).findElements(By.tagName("td"));

            for(int j=0;j<2;j++) {

                String CaseV = Columns.get(j).getText();
                System.out.println(CaseV);

It is fetching me all the values like this:(This is how it comes on console) Case ID:

3412943

Date of Case:

26-09-2018 13:27 PM

Name:

j k

How can i achieve this? Can Somebody help me with this? Let me know if more information is needed?

3
  • Can you please post the HTML code rather than an image. Would like to give a try - but no energy for entering the HTML :( Oct 3, 2018 at 8:20
  • I have added the HTML Code Oct 3, 2018 at 8:29
  • What should you fetch eventually in your ideal case according to given example? You only show what your code fetches.
    – Alexey R.
    Oct 3, 2018 at 11:26

2 Answers 2

1

Another approach would be to get all the <tr> elements and then search each individual <tr> element for both <td> elements. Assuming the page isn`t dynamic/doesn`t change much, the first one will always be the title of the field and second one will be its value.

Here is an example in Python using Selenium:

# dictionary keys are titles as they are on the website, 
# except in lowercase and the ":" is stripped
result_data = {}
tr_elements = webdriver.find_elements_by_css_selector("tr")
for tr_element in tr_elements:
    td_elements = tr_element.find_elements_by_css_selector("td")
    title = td_elements[0].text
    value = td_elements[1].text
    result_data[title] = value.lower().strip(":")
print(result_data)
0

You can use the following XPath to find all td that are followed by td having text CaseID.

/html/body/table/tbody/tr/td[contains(text(), "Case ID")]/following-sibling::td

Just do a getText() on the WebElement to get the numeric value.

1
  • That said if your table contains only one entry, just using /html/body/table/tbody/tr[1]/td[2] might be easier. Oct 3, 2018 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.