简体   繁体   中英

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.


在此处输入图片说明


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)

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)

Try below Subroutine.

Note: You should know the index value of an object you want to click. Try to use Object Spy to know the index of an item. You might not click your desired element if you set the wrong index.

Sub ClickWebTableIndex(BrowserPropertyName, BrowserPropertyValue, PagePropertyName, PagePropertyValue, WebTablePropertyName, WebTablePropertyValue, ChildObjectClassName, ChildObjectPropertyName, ChildObjectPropertyValue, IndexValue)

    Set oBrowser = Description.Create()
    oBrowser("micclass").Value = "Browser"
    oBrowser(BrowserPropertyName).Value = BrowserPropertyValue

    Set oPage = Description.Create()
    oPage("micclass").Value = "Page"
    oPage(PagePropertyName).Value = PagePropertyValue

    Set oWebTable = Description.Create()
    oWebTable("micclass").Value = "WebTable"
    oWebTable(WebTablePropertyName).Value = WebTablePropertyValue

    Set childObject = Description.Create
    childObject("micclass").value = ChildObjectClassName
    childObject(ChildObjectPropertyName).value = ChildObjectPropertyValue

    Browser(oBrowser).Page(oPage).WebTable(oWebTable).ChildObjects(childObject)(IndexValue).FireEvent "OnClick"

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM