循环中的错误

时间:2018-05-15 13:15:05

标签: c# selenium do-while

public Login ClickGetStatus()
{
    //IWebElement btnGetStatus = driver.FindElement(By.XPath("//*[contains(@id,'GetStatus')]"));
    do
    {
        buttonName_GetStatus[0] = "abc";
        Thread.Sleep(3000);

        bool is_displayed = 
            wrapper.IsElementDisplayed(
                driver.FindElement(By.XPath("//*[contains(@id,'GetStatus')]")));

        //bool IsElementDisplayed = driver.FindElement(By.XPath("//*[contains(@id,'GetStatus')]")).Displayed;


        if (is_displayed)
        {
            //wrapper.Click(btnExecute);

            string getnameofbutton1 = 
                driver.FindElement(
                    By.XPath("//*[contains(@id,'GetStatus')]")).GetAttribute("id");

            Console.WriteLine("Name of the button is : " + getnameofbutton1);

            buttonName_GetStatus = getnameofbutton1.Split('_');
            driver.FindElement(
                By.XPath("//*[contains(@id,'GetStatus')]")).Click();
        }
        else
        {
            Console.WriteLine("Element is not displayed");
        }
    }
    while (buttonName_GetStatus[0] == "GetStatus");

    return this;
}

以下是上述代码的逻辑

  • 检查名为Get Status
  • 的按钮
  • 如果找到按钮Get Status然后点击它
  • 我在xpath中使用了contains,因为该按钮的元素id动态变化。
  • 以上代码运行正常并点击了Get Status按钮,但当Get Status按钮的名称更改为View Result并且仍然搜索{时,它不会从循环中出来{1}}按钮

2 个答案:

答案 0 :(得分:1)

如果更新后按钮的预期ID是" ViewResult",那么您可以更新您的条件以使用它。

while (buttonName_GetStatus[0] != "ViewResult");

这将保持循环,而按钮不等于" ViewResult"。

这是您尝试实现的行为吗?

public Login ClickGetStatus()
{
    //IWebElement btnGetStatus = driver.FindElement(By.XPath("//*
    [contains(@id,'GetStatus')]"));
    do
    {
    buttonName_GetStatus[0] = "abc";
    Thread.Sleep(3000);

    var elements = driver.FindElements(By.XPath("//*[contains(@id,'GetStatus')]"));

    var is_displayed = elements.Count > 0;

    //bool IsElementDisplayed = driver.FindElement(By.XPath("//*[contains(@id,'GetStatus')]")).Displayed;


    if (is_displayed)
    {
        //wrapper.Click(btnExecute);

        string getnameofbutton1 = 
            driver.FindElement(
                By.XPath("//*[contains(@id,'GetStatus')]")).GetAttribute("id");

        Console.WriteLine("Name of the button is : " + getnameofbutton1);

        buttonName_GetStatus = getnameofbutton1.Split('_');
        driver.FindElement(
            By.XPath("//*[contains(@id,'GetStatus')]")).Click();
    }
    else
    {
        Console.WriteLine("Element is not displayed");
    }
}
while (buttonName_GetStatus[0] != "ViewResult");

return this;
}

答案 1 :(得分:0)

我认为问题可能就在这里,尤其是当您检查isDisplayed == true是否然后此行buttonName_GetStatus = getnameofbutton1.Split('_');覆盖数组以便出现不定式循环时。