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

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

问题描述

这是我的代码,它能够获取datagrid列的标题并从用户输入中找到匹配的值.例如,如果输入为"jdoe",它将查看数据网格中的用户名"列,并将 [value.Key] .Text value.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天全站免登陆