Search code examples
vbscriptqtphp-uft

How to click on a webtable cell using UFT/VBSCRIPT


I just need to click on a specific cell (2,1). I am not sure how I can click on that cell. In my case, it is always going to be the same cell.

I have this code which just reads the value from cell (2,1). After that, how can I click that cell? Entire cell is a link. I do not need to use for loop to go thru and find value. I just need to click directly on that cell.

myvalue= Browser("aa").Page("aa").WebTable("tb").GetCellData(2,1)

Please advise.


enter image description here


Update # 1 1

myvalue= Browser("aa").Page("aa").WebTable("tb").GetCellData(2,1)

'value returns "dsfsdf sdfsdfdsf DOB: 1/01/2015 Member ID: 8587"

2 I inspected the cell in chrome. I see this:

<td class="name" rowspan="1">
                                            <a href="/id/5858"><span></span></a>
                                            <h3>
                                            dsfsdf sdfsdfdsf
                                            </h3>
                                            <table>
                                                <tbody><tr>
                                                    <td>DOB:</td>
                                                    <td>1/01/2015</td>
                                                </tr>
                                                <tr>
                                                    <td>Member ID:</td>
                                                    <td>8587</td>
                                                </tr>
                                            </tbody></table>
                                        </td>
  1. In front end, i see the entire cell is a link but in source code you can see only one element is a link. I think they have applied css to make the cell as a link.

SHould I first go to the cell then find the link and then click on it? I researched all day but no luck. Please advise.


Update # 2

This is a dot net page.

set rowObject = Browser("aa").Page("aa").WebTable("tb").Object.getElementsByTagName("tr").item(1) 

If I keep item(1) it clicks on the 2nd row as expected. index 0 is the header. index 1 is the 2nd row.

If I change to item(2), it does nothing.

If I change to item(4), it clicks 3rd row(index 2). Instead it should click 5th row (index 4).

If I change to item(5), it does nothing. Weird. So this answer is not working consistently. I think UFT is being confused way that the page was developed.

I took the html source code and then created a html file locally. Changed all anchor texts from empty to some texts:

            <a href="/id/5554"><span>id2</span></a>

Then I used your original code that you posted. It works just fine as expected for all rows.

I will reach out to dev team to see if they can add anchor text and then apply css to hide it. Most likely they wont do anything.

Also can we describe the "Link" here to "a" tag so UFT will look for "a" tag instead of link texts/anchor texts.

set objLink = Browser("aa").Page("aa").WebTable("tb").ChildItem(intRow, intCol, "Link",0)

Solution

  • Use the childitem method of a webTable.

    Try something like:

    Dim objLink, intRow, intCol
    intRow = 2
    intCol = 1
    set objLink = Browser("aa").Page("aa").WebTable("tb").ChildItem(intRow, intCol, "Link",0)
    if objLink.exists(3) then
        objLink.Click
    end if
    

    Update 2:

    Since the link present inside the webtable's cell does not have any text associated with it, UFT is not able to find it using the ChildItem method. This can also be verified by using the ChildItemCount method of the webtable. Hence, we now need to use object's native methods as shown below:

    set rowObject = Browser("aa").Page("aa").WebTable("tb").Object.getElementsByTagName("tr").item(1)      'HERE item(1) means the 2nd item or 2nd row. It is a zero-based index, hence for 2nd row, we write index=1
    set colObject = rowObject.getElementsByTagName("td").item(0)     'item(0) means 1st item or 1st column as it is a zero based index
    Set collectionLinks = colObject.getElementsByTagName("a")        'getting a collection of all the links present inside cell(2,1)
    collectionLinks.item(0).Click                                    'item(0) means the 1st link in the cell(2,1)