如何使用UFT/VBSCRIPT单击webtable单元格

如何使用UFT/VBSCRIPT单击webtable单元格,vbscript,qtp,hp-uft,Vbscript,Qtp,Hp Uft,我只需要点击一个特定的单元格(2,1)。我不知道如何点击那个单元格。在我的情况下,它永远是同一个细胞 我有一段代码,它只从单元格(2,1)读取值。之后,我如何单击该单元格?整个单元格是一个链接。我不需要使用for循环来遍历和查找值。我只需要直接点击那个单元格 myvalue= Browser("aa").Page("aa").WebTable("tb").GetCellData(2,1) 请告知 更新#1 一, '值返回“dsfsdf sdfsdfdsf DOB:1/01/2015成员I

我只需要点击一个特定的单元格(2,1)。我不知道如何点击那个单元格。在我的情况下,它永远是同一个细胞

我有一段代码,它只从单元格(2,1)读取值。之后,我如何单击该单元格?整个单元格是一个链接。我不需要使用for循环来遍历和查找值。我只需要直接点击那个单元格

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



更新#1 一,

'值返回“dsfsdf sdfsdfdsf DOB:1/01/2015成员ID:8587”

2我检查了镀铬的电池。我看到:

<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),它会按预期单击第二行。索引0是标题。索引1是第二行

如果我改为第(2)项,它什么也不做

如果我更改为第(4)项,它将单击第三行(索引2)。相反,它应该单击第5行(索引4)

如果我改为第(5)项,它什么也不做。奇怪的因此,这个答案并不是一成不变的。我认为UFT对页面的开发方式感到困惑

我获取了html源代码,然后在本地创建了一个html文件。将所有锚文本从空更改为某些文本:

            <a href="/id/5554"><span>id2</span></a>
使用webTable的方法

尝试以下方法:

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
更新2:

由于webtable单元格中的链接没有任何与之关联的文本,UFT无法使用
ChildItem
方法找到它。这也可以通过使用webtable的
ChildItemCount
方法进行验证。因此,我们现在需要使用对象的本机方法,如下所示:

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)
试试下面的子程序

注意:您应该知道要单击的对象的索引值。尝试使用Object Spy了解项目的索引。如果设置了错误的索引,则可能不会单击所需的元素

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

很高兴提供帮助:)我昨天多次运行了上述代码。成功了。我今天跑了很多次。它一点也没有点击。我完全被UFT搞糊涂了。有时是不一致的。我重新启动了机器。再次打开应用程序。应用程序看起来完全一样。我不确定会出什么问题。一切都没有改变。它应该会起作用。现在不是了。请告知。就在If语句之前,编写
msgbox objLink.exists(2)
并运行代码。让我知道显示的是msgbox,上面写着“需要对象”。请看图片。好吧,这只是意味着单元格内不再有链接。您可以手动单击该链接吗?
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
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)
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