简体   繁体   English

使用 Selenium WebDriver c# 检索表数据

[英]Retrieving table data using Selenium WebDriver c#

Sorry I am still new to c# and selenium programming.抱歉,我对 c# 和 selenium 编程还很陌生。

I am in the process of automating an application that contains a dynamic table.我正在自动化包含动态表的应用程序。 The table consists of 4 columns, 3 columns contain data and 1 column contains 2 buttons for each row.该表由 4 列组成,3 列包含数据,1 列包含每行 2 个按钮。

When writing my Selenium test I want to pass in a string of 'Username', then have some smart code to retrieve data from the table and match against the passed in String.在编写我的 Selenium 测试时,我想传入一个“用户名”字符串,然后使用一些智能代码从表中检索数据并匹配传入的字符串。 Once found then I want it to go across the row and select either of the 2 buttons on that row.找到后,我希望它穿过该行并选择该行上的 2 个按钮中的任何一个。

<table>
 <tbody>
  <tr>
    <td>UserName_Data</td>
    <td>Email_data</td>
    <td>IsDeactivated</>
    <td>[blank]</td>
        <a class="xxx" title="Edit User Account" href="xxx"/>
        <a class="xxx" title="Deactivate User" href="xxx"/>
    </td>
  </tr>
  <tr>
    <td>UserName_Data</td>
    <td>Email_data</td>
    <td>IsDeactivated</>
    <td>[blank]</td>
        <a class="xxx" title="Edit User Account" href="xxx"/>
        <a class="xxx" title="Deactivate User" href="xxx"/>
    </td>
  </tr>
  <tr>
    <td>UserName_Data</td>
    <td>Email_data</td>
    <td>IsDeactivated</>
    <td>[blank]</td>
        <a class="xxx" title="Edit User Account" href="xxx"/>
        <a class="xxx" title="Deactivate User" href="xxx"/>
    </td>
  </tr>
 </tbody>
</table>


[TestMethod]
        public void NavigateToEditManageUsers()
        {
            ManageUsersPage.GoTo();
            ManageUsersPage.EditUser(Bill);
        }

I removed the [blank] td from your example for the purpose of my answer because you described the <a> tags being in the 4th column:出于回答的目的,我从您的示例中删除了 [blank] td,因为您描述了<a>标签位于第 4 列中:

<table>
<tbody>
<tr>
    <td>UserName_Data</td>
    <td>Email_data</td>
    <td>IsDeactivated</td>
    <td>
        <a class="xxx" title="Edit User Account" href="xxx"/>
        <a class="xxx" title="Deactivate User" href="xxx"/>
    </td>
</tr>
<tr>
    <td>Bill</td>
    <td>Email_data</td>
    <td>IsDeactivated</td>
    <td>
        <a class="xxx" title="Edit User Account" href="xxx"/>
        <a class="xxx" title="Deactivate User" href="xxx"/>
    </td>
</tr>
<tr>
    <td>UserName_Data</td>
    <td>Email_data</td>
    <td>IsDeactivated</td>
    <td>
        <a class="xxx" title="Edit User Account" href="xxx"/>
        <a class="xxx" title="Deactivate User" href="xxx"/>
    </td>
</tr>
</tbody>
</table>

The following will find the second row containing the username "Bill" and then get the first <a> tag:下面将找到包含用户名“Bill”的第二行,然后获取第一个<a>标签:

//table/tbody/tr[td[1] = "Bill"]/td[4]/a

Here is an implementation in C#:这是 C# 中的实现:

public class ManageUsersPage
{
    public static void GoTo()
    {
        // Loads page
    }

    public static void EditUserAccount(string username)
    {
        Driver.FindElement(By.XPath("//table/tbody/tr[td[1] = '" + username + "']/td[4]/a[1]")).Click();
    }

    public static void DeactivateUser(string username)
    {
        Driver.FindElement(By.XPath("//table/tbody/tr[td[1] = '" + username + "']/td[4]/a[2]")).Click();
    }

}

[TestMethod]
public void NavigateToEditManageUsers()
{
    ManageUsersPage.GoTo();
    ManageUsersPage.EditUserAccount("Bill");
}

This looks like a job for the TableDriver extension to .NET WebDriver ( https://github.com/jkindwall/TableDriver.NET ).这看起来像是将 TableDriver 扩展到 .NET WebDriver ( https://github.com/jkindwall/TableDriver.NET ) 的工作。 In the above example, it could be used like this:在上面的例子中,它可以这样使用:

public void UserAction(string username, string action)
{
    Table table = Table.CreateWithNoHeaders(driver.FindElement(By.TagName("table")), 0); // Assumes this is the only table on the page.
    TableCell cell = table.FindCell($"0={username}", 3)
    if (action == "edit")
    {
        cell.Element.FindElement(By.CssSelector("a[title='Edit User Account'")).Click();
    }
    else if (action == "deactivate")
    {
        cell.Element.FindElement(By.CssSelector("a[title='Deactivate User'")).Click();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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