【问题标题】:How to click on a webtable cell using UFT/VBSCRIPT如何使用 UFT/VBSCRIPT 单击 webtable 单元格
【发布时间】:2018-02-26 18:15:21
【问题描述】:

我只需要点击一个特定的单元格 (2,1)。我不确定如何单击该单元格。就我而言,它始终是同一个单元格。

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

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

请指教。



更新 #1 1

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

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

2 我检查了 chrome 中的单元格。我看到了:

<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. 在前端,我看到整个单元格是一个链接,但在源代码中你只能看到一个元素是一个链接。我认为他们已经应用 css 将单元格作为链接。

我应该先转到单元格然后找到链接然后单击它吗?我研究了一整天,但没有运气。请指教。


更新 #2

这是一个点网页面。

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

如果我保留 item(1),它会按预期点击第二行。索引 0 是标题。索引 1 是第 2 行。

如果我更改为 item(2),它什么也不做。

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

如果我更改为 item(5),它什么也不做。诡异的。所以这个答案并不一致。我认为 UFT 对页面的开发方式感到困惑。

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

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

然后我使用了您发布的原始代码。它对所有行都按预期工作。

我会联系开发团队,看看他们是否可以添加锚文本,然后应用 css 将其隐藏。他们很可能不会做任何事情。

我们还可以将此处的“链接”描述为“a”标签,以便 UFT 将查找“a”标签而不是链接文本/锚文本。

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

【问题讨论】:

    标签: vbscript qtp hp-uft


    【解决方案1】:

    使用 webTable 的 childitem 方法。

    尝试类似:

    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)
    

    【讨论】:

    • 很高兴为您提供帮助:)
    • 我昨天多次运行上述代码。有效。我今天跑了很多次。它根本没有点击。我完全对 UFT 感到困惑。有时并不一致。我重新启动了机器。再次打开应用程序。应用程序看起来一样。我不确定会出什么问题。什么也没有变。它应该工作。现在它没有。请指教。
    • 就在 If 语句之前,编写 msgbox objLink.exists(2) 并运行代码。让我知道显示的是 msgbox。
    • 它说“需要对象”。请看图片。
    • 嗯,这只是意味着单元格内部不再有链接。你能手动点击那个链接吗?
    【解决方案2】:

    试试下面的子程序。

    注意:您应该知道要点击的对象的索引值。尝试使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-10
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多