Highlighted
Valued Contributor.. Valued Contributor..
Valued Contributor..
264 views

Cannot get text inside sibling table cell using WebElement.Object.previousSibling.innerText

Environment: Windows 10 Enterprise (64-bit), UFT 14.03 with Web Add-In,  Firefox 60.6.2esr (32-bit) 

Given the following dynamically-generated table where  the table exists in the Object Repository as .WebTable("mytable"):

<table id="mytable" class="table table-hover"> 
  <thead>
     <tr>
        <th> Col 1</th>
        <th> Col 2</th>
        <th> Col 3</th>
        <th> Col 4</th>
        <th> Col 5</th>
    </tr>
</thead>
<tbody>
    <tr class="ng-scope">
       <td class="ng-binding">Text Data 1</td>
       <td class="ng-binding">Text Data 2</td>
       <td class="ng-binding">Text Data 3</td>
       <td class="ng-binding">Text Data 4</td>
       <td class="ng-binding">Text Data 5</td>
    </tr>
    <tr class="ng-scope">
       <td class="ng-binding">Text Data 6</td>
       <td class="ng-binding">Text Data 7</td>
       <td class="ng-binding">Text Data 3</td>
       <td class="ng-binding">Text Data 9</td>
       <td class="ng-binding">Text Data 10</td>
    </tr>
  </tbody>
</table>

I'm able to use the following descriptive programming logic to find the child objects (i.e., <td> cells) in the table containing "Text Data 3". However, when looping through each "Text Data 3" child object and trying to find and click on the one that has a previous sibling table cell (oChildObj.Object.previousSibling)  having innerText, = "Text Data 7" isn't working. I've tried using the .innertext, .innerHTML, or .textContent  DOM properties on the previous sibling cell  and each property returns a blank/empty value. Hence, the intended "Text Data 3" object is never clicked/selected. Even when I comment out the If StrComp  and End If lines, leaving the three print statements uncommented out just to see the values, each print statement displays blank/empty value (.innerText and innerHTML) or blank/empty value followed by line feed.  

Am I using oChildObj.Object.previousSibling.innerText (i.e., I tried using innertext and innerText, still doesn't work) correctly to get the text inside the sibling cell (e.g. <td>Text Data 7</td>).  oChildObj.Object should return the native HTML DOM object for the <td>Text Data 3</td> and previousSibling method/property should return the previous sibling object. If the .innertext, innerHTML, nor textContent properties on the sibling object doesn't return the text data inside the sibling table cell,  I'm not sure how else to get the text value to use in the If StrComp statement.

Thanks in advance!

 

Dim oDesc, i, objCnt, cellValue, sibCellValue
Dim oCollection, oChildObj, oSiblingObj

cellValue = "Text Data 3"
sibCellValue = "Text Data 7"

Set oDesc = Description.Create
oDesc("micclass").Value = "WebElement"
oDesc("class").Value = "ng-binding"
oDesc("innertext").Value = cellValue
oDesc("visible").Value = True

Set oCollection = .WebTable("mytable").ChildObjects(oDesc)
objCnt = Cint(oCollection.Count)

If objCnt > 0 Then
   For i = 0 to objCnt - 1
        Set oChildObj = oCollection(i)
        Set oSiblingObj = oChildObj.Object.previousSibling

         print "oSiblingObj innerText " & oSiblingObj.innerText
         print "oSiblingObj innerHTML " & oSiblingObj.innerHTML
         print "oSiblingObj textContent " & oSiblingObj.textContent

         If StrComp(oSiblingObj.innerText,sibCellValue) = 0 Then
             oChildObj.Click
            Exit For
        End If
   Next
End If

<table id="mytable" class="table table-hover"> 
  <thead>
     <tr>
        <th> Col 1</th>
        <th> Col 2</th>
        <th> Col 3</th>
        <th> Col 4</th>
        <th> Col 5</th>
    </tr>
</thead>
<tbody>
    <tr class="ng-scope">
       <td class="ng-binding">Text Data 1</td>
       <td class="ng-binding">Text Data 2</td>
       <td class="ng-binding">Text Data 3</td>
       <td class="ng-binding">Text Data 4</td>
       <td class="ng-binding">Text Data 5</td>
    </tr>
    <tr class="ng-scope">
       <td class="ng-binding">Text Data 6</td>
       <td class="ng-binding">Text Data 7</td>
       <td class="ng-binding">Text Data 3</td>
       <td class="ng-binding">Text Data 9</td>
       <td class="ng-binding">Text Data 10</td>
    </tr>
  </tbody>
</table>

 

0 Likes
1 Reply
Highlighted
Valued Contributor.. Valued Contributor..
Valued Contributor..

Re: Cannot get text inside sibling table cell using WebElement.Object.previousSibling.innerText

I was able to successfully get the text inside the sibling table cell by using the WebElement's .Object.previousElementSibling.innertext instead of .Object.previousSibling.innertext because previousSibling was likely returning an empty text node (i.e., presumably the whitespace between the sibling's WebElement's closing </td> and the initial WebElement's starting <td> tag.  Using previousElementSibling ensures that it will return an the previous Element node as the sibling. 

The code is now working as intended.

 

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.