【问题标题】:Clicking on a dynamic link in selenium单击硒中的动态链接
【发布时间】:2011-01-16 11:12:00
【问题描述】:

我需要点击动态生成 id 的表中的链接。我想单击基于同一行中其他列中的文本的链接。尝试了以下代码但不成功

selenium.GetValue("//table[@id=TableID]/tbody/tr[td/a/text()='Testing']")

还尝试使用以下代码 selenium.click("xpath=id(TableID)/tbody/tr[td/text()='Testing']//输入[@value='Delete']")

尝试了以下代码,但只能通过指定静态行 id 来工作

 Dim NumOfRows As Integer = selenium.GetXpathCount("//table[@id='up']/tbody/tr")
        'Dim index As Integer
        Dim ReturnValue As Integer
        Dim IsFound As Boolean = False
        Console.WriteLine(NumOfRows)

        For i As Integer = 1 To NumOfRows
            Dim strColumnText As String = selenium.GetText("//table[@id='up']/tbody/tr[i]/td[1]")
            'selenium.WaitForCondition(strt, 100000)
            If (strColumnText = pord) Then
                ReturnValue = i
                IsFound = True
                Exit For
            End If
        Next

如果我指定确切的行,它会找到元素,但不会循环工作。请帮忙

【问题讨论】:

  • 对于作为答案的 XPath 表达式,您需要提供输入样本。

标签: vb.net visual-studio-2010 xpath selenium


【解决方案1】:

如果值在表中,您应该使用

selenium.getTable("tableName.0.1");

0 代表行,1 代表列(两者都是基于 0 的索引)

如果成功,getTable 会返回一个字符串:"OK,value" 所以你需要去掉 OK, 部分。

更新 添加到 user-extensions.js

    Selenium.prototype.getTableRows = function(locator) {
  /**
   * Gets the number of rows in a table.
   *
   * @param locator element locator for table
   * @return number of rows in the table, 0 if none
   */

    var table = this.browserbot.findElement(locator);
    return table.rows.length.toString();
};

硒 RC

// Table Name in inputParams

    String[] inputParams = { "tblTryggHuvudlantagare", };

                String y = proc.doCommand("getTableRows", inputParams);
                String tmpString = y.replace("OK,", "");

                // Rows minus headings


    int tableRows = Integer.valueOf(tmpString).intValue() - 1;

        for (int i = 1; i < tableRows; i++) {
    // Your logic here to see if you reached the right row and then click button on same row..
                    }

【讨论】:

  • 在这里您要指定确切的行和列,但我必须根据值“测试”选择一行,然后单击另一列中同一行的删除按钮。任何想法如何做到这一点..
  • 我曾经做过一个解决方案,我必须迭代表以找到我的值来获取行号。从那里我猜你现在删除按钮是哪一列。我必须制作一个 JavaScript 来获取表的大小,因为如果你尝试不存在的行,Selenium 会抛出异常。看看我是否找到我的解决方案并将其添加到我的答案中。
  • 尝试使用您使用以下代码指定的选项 For i As Integer = 1 To NumOfRows Dim strcolumntable As String = selenium.GetTable("ctl00_cphMain_gvOperatorGroup.i.1") 'selenium.WaitForCondition(strColumnText, 100000) If (strcolumntable = pKey) Then ReturnValue = i IsFound = True Exit For End If Next 使用两个代码获得相同的问题...不遍历循环。如果提供静态行值可以正常工作但不能迭代。任何帮助请
  • selenium.GetTable("ctl00_cphMain_gvOperatorGroup.i.1") 应该不是 selenium.GetTable("ctl00_cphMain_gvOperatorGroup." + i + ".1")
  • 感谢 Stefan。是的也尝试过,但是从字符串转换为双精度类型无效。尝试以下 For i As Integer = 2 To NumOfRows If (selenium.GetTable("Group." + i + ".1") = pKeyword) Then ReturnValue = i IsFound = True Exit For End If Next 知道我为什么会得到这个
猜你喜欢
  • 2013-09-07
  • 2016-05-17
  • 1970-01-01
  • 2023-03-07
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多