1

I have a report which is completely of HTML based. I'm trying to automate and using selenium webdriver for finding the elements. In one of the part, below is the HTML format

<tbody>  
<tr>
  <td style="border: 1px solid #D1D1D1; background-color:#F4F4F4; color: #707070;padding :2px;">
    <table style="width: 100%;
                border-collapse: collapse; font-size: 15px;font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;" role="presentation"> 
          <tbody><tr>
  <!--Action Date -->
            <td></td>
            <td width="100%" valign="top" align="right">
                <span style="font-size: 12px;">12/7/20 10:21 AM</span>
            </td>
  </tr>
         <tr>
        <!-- Name and status column -->
            <td style="" width="25" valign="top" align="center">
              <img src="images/qual_checkmark_24.png" alt="">
             </td>
            <td width="535">
                <span>First line of <strong>TEXT</strong></span>
             </td>
        </tr>
             
        </tbody></table>
</td>
</tr>
<tr>
  <td style="border: 1px solid #D1D1D1; background-color:#F4F4F4; color: #707070;padding :2px;">
    <table style="width: 100%;
                border-collapse: collapse; font-size: 15px;font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;" role="presentation"> 
        
          <tbody><tr>
  <!--Action Date -->
            <td></td>
            <td width="100%" valign="top" align="right">
                <span style="font-size: 12px;">12/7/20 10:18 AM</span>
            </td>
  </tr>
         <tr>
        <!-- Name and status column -->
            <td style="" width="25" valign="top" align="center">
              <img src="images/qual_arrowupcircleblue_24.png" alt="">
             </td>
            <td width="535">
                <span>Second line of<strong>TEXT</strong></span>
             </td>
        </tr>     
        </tbody>
        </table>
</td>
</tr>
</tbody>

I wanted to find the text in span tag (eg: first line of text, second line of text ) where i don't have any other attributes. I tried various other way of finding it via xpath, cssSelector and nothing works. Could someone help?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

4 Answers4

1

Try to use this XPath to select span nodes without attributes:

//span[not(@*)]
JaSON
  • 4,843
  • 2
  • 8
  • 15
1
//tbody/tr[2]/td/span

you can use the parent as reference, in the html you provided the span is under the tr which is second element of tbody.

//tbody/tr[2] , will find all elements which are second child of tbody , and then direct child td/span

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • @this select only second td , as only this has span as child , and it's direct child also . So only that will be selected not first td – PDHide Dec 07 '20 at 16:16
0

A combination of both answers would be probably best:

//tbody/tr/td/span[not(@*)]

It's still flexible and less error-prone at the same time.

wp78de
  • 18,207
  • 7
  • 43
  • 71
0

There are two sets of <span> tags. One that holds the datetime e.g. 12/7/20 10:18 AM and the other that holds the text e.g TEXT.

To print the list of <span> tag texts you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • xpath:

    List<String> myTexts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//tbody//tr/td/table/tbody//tr//td/span[./strong]"))).stream().map(element->element.getText()).collect(Collectors.toList());
    System.out.println(myTexts);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352