如何使用C#在Selenium webdriver中处理动态ID

时间:2016-08-30 21:15:23

标签: c# selenium

我正在自动化我们的应用程序,并且它具有在表单中动态的ID。当我点击按钮在页面中添加另一个表单时会发生什么,HTML显示如下:

<table id = "expense_table">
<div id = "OtherExpense">
<input id = "Form01_dropdown01"></input>
<div id = "OtherExpense">
<input id = "Form02_dropdown01"></input>
<input id = "Form02_dropdown02"></input>
</table>

Form01递增,以及单击添加按钮时的dropdown01。我曾尝试使用增量,因此每次单击添加按钮时,Form01将递增为2.

    public int mainCtr = 0; // this increments when the add button is clicked
    public int formctr = 0;
    public int dropcTr = 0;
 IList<IWebElement> elements = driver.FindElements(By.XPath(".//*[contains(@id,'dropdown')]")); //counting dropdown boxes that are existing in the TA form
        int intLinkCount = elements.Count; // get the number of the dropdown boxes that are existing in the TA form
        dropdownCount = intLinkCount;

        if (dropdownCount > 0)
        {

                dropdownCount = dropdownCount + 1;
                dropCtr = dropdownCount;

        }
        else
        {
            dropCtr = dropCtr + 2;
        }
    if(formctr > 0)
    {
     dropcTr = dropcTr + 1;
    }

    driver.FindElement(By.Id(String.Format("Form0{0}_Button", otherExpCtr))).Click(); // button to add the dropdown below
    var otherexpInput = driver.FindElement(By.Id(String.Format("Form0{0}_dropdown0{1}", formCtr, dropcTr)));

每次点击按钮时,新表单都会添加到页面中,当点击下拉列表的添加按钮时,也会添加一个新的下拉列表。我不知道如何处理它,因为会发生什么当Form01上有1个现有下拉列表时,dropdownCount的值将变为1,使其成为分配给dropCtr的2。单击添加下拉列表时,它将查找尚未存在的dropDown02,因为它刚刚添加了dropDown01。 谢谢!

0 个答案:

没有答案