根据条件

时间:2017-07-18 10:11:48

标签: c# selenium

我有一个记录列表,例如状态,部门,金额,标题和一些按钮。 如果我想断言标题,我使用了以下代码:

private IWebElement GetTitleElement(int actionRowNumber)
    {
        var xpath = $"//tr[@id = 'ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__{actionRowNumber}']/td[9]";
        return BrowserFactory.Driver.FindElement(By.XPath(xpath));
    }

我对类型,状态,部门做同样的事情

然后我使用try catch方法遍历每条记录,直到我在系统中找到所需的记录:

public void CheckActionxxx(ActionTypes type, Enum.Departments departments, Enum.ActionStatus actionstatus, string title)
    {
        Wait();
        var actiontype = type;
        try
        {
            for (var i = 0; i <= 4; i++)
            {
                if (GetTitleElement(i).Text == title && GetActionTypeElement(i).Text == actiontype.ToString())
                {
                    Assert.AreEqual(type.ToString(), GetActionTypeElement(i).Text);
                    Assert.AreEqual(actionstatus.ToString(), GetActionStatusElement(i).Text);
                    Assert.AreEqual(departments == Enum.Departments.None ? " " : departments.ToString(),
                    GetActionDepartmentElement(i).Text);
                    return;
                }
            }
            throw new NoSuchElementException();
        }
        catch (NoSuchElementException)
        {
            throw new NoSuchElementException($"Not found with'{title}'");
        }
    }

因为id只有数字上的差异(例如0,1,2)我能够使用整数

系: // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__0”] / TD [5] // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__1”] / TD [5] // * [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__2”] / TD [5]

状态: // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__0”] / TD [2] // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__1”] / TD [2] // * [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__2”] / TD [2]

名称: // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__0”] / TD [9] // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__1”] / TD [9] // * [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00__2”] / TD [9]

现在我想使用try catch找到一个基于相同工作方式的按钮,但是这个按钮的id如下: // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_ctl04_ButtonClose”] // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_ctl06_ButtonClose”] // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_ctl08_ButtonClose”] // [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_ctl10_ButtonClose”] // * [@ ID = “ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_ctl12_ButtonClose”]

正如你所看到的那样,它从ctl04开始,并以两步为单位继续。

我无法弄清楚如何设计我的私人元素。我试过了:

private IWebElement GetActionCloseButtonElement(int actionRowNumber)
    {
        var xpath = $"//tr[@id = 'ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_{actionRowNumber}_ButtonClose']";
        return BrowserFactory.Driver.FindElement(By.XPath(xpath));
    }

但那不起作用。

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效。

actionRowNumber应该是04,06,08,10,12,与xpath相同。

private IWebElement GetActionCloseButtonElement(int actionRowNumber)
{
return BrowserFactory.Driver.FindElement(By.Xpath("//tr[@id = 'ctl00_ctl00_CPH_CPH_ActionsOverviewControl1_RadGridActions_ctl00_ctl"+ actionRowNumber +"_ButtonClose']"));
}