如何在 C# 中找到正确的用户名后单击数据网格中的按钮 [英] How to Click on a Button within a datagrid after finding the correct username in C#

查看:12
本文介绍了如何在 C# 中找到正确的用户名后单击数据网格中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的代码,它能够获取数据网格列的标题并从用户输入中找到匹配的值.例如,如果输入是jdoe",它将查看数据网格中的用户名列并将 [value.Key].Textvalue.Value 匹配.现在的问题是每一行都有一个具有相同自动化 ID 的编辑"按钮.我如何遍历数据网格并能够单击编辑"按钮,而不管jdoe"在哪一行:这是我目前所拥有的:

So here is my Code that is able to get the header of the datagrid column and find the matching values from the user inputs. For Example if input is "jdoe" it will look at the Username column in the datagrid and match the [value.Key].Text to the value.Value. Now the problem is that each row has an "Edit" button with the same automation ID. How do I iterate through the datagrid and be able to click on the Edit button with regardless of what row "jdoe" is in: Here is what I have so far:

    public static bool Contains(this ListView listView, ObjectInList  objectInList)
    {
        foreach (ListViewRow row in listView.Rows)
        {

            if (DataMatches(row, objectInList))
                return true;
        }

        return false;
    }


    private static bool DataMatches(ListViewRow row, ObjectInList objectInList)
    {
        foreach (KeyValuePair<string, string> value in objectInList.Values)
        {
            if (row.Cells[value.Key].Text != value.Value)


                return false;


        }
        return true;
    }
}


internal class UserInList : ObjectInList
{



    public UserInList(string username)
    {

        _values["Username"] = username;


    }
}

internal class OrderInList : ObjectInList
{

    public OrderInList(string orderNumber)
    {

        _values["Depot Tag #"] = orderNumber;


    }
}

internal abstract class ObjectInList
{

    protected readonly Dictionary<string, string> _values = new Dictionary<string, string>();

    public IReadOnlyDictionary<string, string> Values
    {

        get { return _values; }
    }

这是WPF应用的截图

推荐答案

这是我用来遍历表的内容:

Here is what I use to iterate through tables:

获取您的搜索元素 = value2

get your search element = value2

获取您的表 ID、xpath 或 css = myTable

get your table id, xpath or css = myTable

循环将遍历表,然后找到 value2.从这里您可以选择下一步要做什么.假设您的列如下所示:

The loop will go through the table and then find value2. From here you have a choice on what you want to do next. Let's say your column looks like this:

|col 1 |col  2  | col   3 |
|link1 | value1 | link2   |
|link1 | value2 | link2   |

在下面它将在 value2 处停止.那变成了 tds[i].单击链接 1 我使用:

In the below it will stop on value2. That becomes tds[i]. To click link 1 I use:

 tds[i - 1].Click(); 

如果我想点击链接 2,我使用:

If I want to click on link 2 I use:

 tds[i + 1].Click(); 

只需考虑 [i] 中的列号并计算左(减)或右(加)列数.

Just consider the column number from [i] and count left (minus) or right (plus) the number of columns.

 public void ClickTableLink(String value2)
  {
  var table =  driver.FindElement(By.Id("myTable"));
  foreach (var tr in table.FindElements(By.TagName("tr")))
  {
   var tds = tr.FindElements(By.TagName("td"));
    for (var i = 0; i < tds.Count; i++)
    {
        if (tds[i].Text.Trim().Contains(value2))
        {
            tds[i - 1].Click();
           break;
        }

    }
  }
}

这篇关于如何在 C# 中找到正确的用户名后单击数据网格中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆