【问题标题】:Checking a box in one Column using a value from another column in the same Table with Selenium C#使用 Selenium C# 使用同一表中另一列中的值检查一个列中的框
【发布时间】:2017-10-10 16:27:16
【问题描述】:

我正在使用 C# (ChromeDriver) 中的 Selenium 使用 NUnit 进行测试并遇到了障碍。我们有一个表格,可以根据为该帐户启用的内容动态加载各种产品。 我正在尝试围绕它编写测试以订购每个单独的产品,但我在标记一个复选框时遇到了障碍。该表仅使用数字 ID 和用于产品名称的单独列(请参阅下面的 HTML 代码)。

有一个包含产品名称的隐藏字段,但过去我被告知 Selenium 无法看到它,除非它是可见的。

任何帮助或想法都是我解决这个问题的一个很好的开始。

<td align="center" valign="top">
    <input id="reports11.selected1" name="reports[11].selected" onclick="javascript:doCheckbox(11, false)" style="margin-top:5px" type="checkbox" value="true">
    <input type="hidden" name="_reports[11].selected" value="on">
    <input id="reports11.productClassId" name="reports[11].productClassId" type="hidden" value="1">
    <input id="reports11.productId" name="reports[11].productId" type="hidden" value="99599">
    <input id="reports11.name" name="reports[11].name" type="hidden" value="Product Build - TEST">
</td>

<td align="center" valign="top">
    <select id="reports11.priority" name="reports[11].priority" style="width:40px;padding:1px;margin: 0 0 0 0" onchange="javascript:doPriority(11)">
        <option value="0" selected="selected">--</option>
    </select>
</td>

<td valign="top">
    Product Build - TEST
</td>

【问题讨论】:

  • 如果您只需要隐藏字段的值,应该很容易。首先使用一些 GetElementBy 方法获取 IWebElement,然后在您获得的 IWebElement 上调用 GetAttribute("value"),然后您可以使用 Selenium 帮助器进行 select => SelectElement 及其 SelectByValue 方法。
  • 您的代码尝试在哪里?您引用“参见下面的代码”,但没有。您需要更具体地说明您正在尝试做什么以及什么不起作用。您引用“标记一个已选中的框”,但您没有说明是哪一个……有 5 个输入和一个选择。 “产品构建 - 测试”应该是产品名称吗?您有很好的示例 HTML...现在在您的问题中添加对该 HTML 的引用以清除您的问题。
  • @JeffC 抱歉,打错字了。基本上,在 HTML 表中,有一个复选框,我可以通过自动化使用 Selenium 进行检查(有 50 个带有复选框的产品)。我正在尝试做的是根据 Product Build - TEST 行中显示的产品名称仅选中其中一个复选框。当我尝试这个 Selenium 时,随着 Reports(X) 的变化,无法看到上述区域中的复选框。
  • 那里有 5 个 INPUT 标签... 4 个被隐藏,但一个没有...您尝试过点击/与那个标签交互吗?
  • 是的,这行得通。但是,我还必须为动态表编写它,因此虽然上面说 Reports11 基本上是“产品构建 - 测试”,但在下一次测试中,它可能是“产品构建 - 测试”的 Reports16。所以我可以在隐藏的输入标签中看到名称,但是如果我尝试调用它,Selenium 看不到它。

标签: c# google-chrome selenium nunit selenium-chromedriver


【解决方案1】:

下面的代码假定第一列的复选框和第三列的产品名称。

public void chooseProductbyName(productName)
{
 string xpath = "//tr[td[3][text()='"+productName+"']]/td[1]/input[@type='checkbox']";
 driver.findElement(by.xpath(xpath)).click();
}

【讨论】:

  • 谢谢!我只是想不通如何使用动态生成的列表来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
相关资源
最近更新 更多