0
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;
}

Below is the Logic for the above code

  • Checks for the button called Get Status
  • if it finds the button Get Status then clicks on it
  • i have used contains in the xpath as the element id for that button changes dynamically.
  • The above code runs fine and clicks on the Get Status button but doesn't come out from the loop when the name of the Get Status button changes to View Result and still searches for Get Status button
Hargovind
  • 73
  • 11
  • 2
    So just to clarify, when the button with the ID "GetStatus" is found the click event is triggered, which in turn updates the ID to be "ViewResult". Is that correct? If so your loop isn't going to exit because you are specifically checking for the value "GetStatus". – Jason Anderson May 15 '18 at 13:22
  • What is the value of `buttonName_GetStatus[0]` and `buttonName_GetStatus.Length` at the end of the loop (just before the `while`)? – mjwills May 15 '18 at 13:23
  • yes jason you are correct. so can you please guide me how to exit from the loop once the loop satisfies the condition of "Get Status" button because the scripts clicks on get status button till its visible but once the button name gets changed the script still looks for the "Get Status" button and fails. – Hargovind May 15 '18 at 13:33
  • If the expected ID of the button after being updated is "ViewResult", then you can update your condition to use that. `while (buttonName_GetStatus[0] != "ViewResult");` This will keep looping round whilst the button does not equal "ViewResult". Is this the behavior you're trying to achieve? – Jason Anderson May 15 '18 at 13:46

2 Answers2

1

If the expected ID of the button after being updated is "ViewResult", then you can update your condition to use that.

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

This will keep looping round whilst the button does not equal "ViewResult".

Is this the behavior you're trying to achieve?

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;
}
  • i am still getting the same error message "OpenQA.Selenium.NoSuchElementException : Unable to find element with xpath == //*[contains(@id,'GetStatus')]" after the button name gets changed – Hargovind May 15 '18 at 14:39
  • i want to my loop to stop once the button name gets updated to "View Result" and after that exit from the loop successfully – Hargovind May 15 '18 at 14:49
  • Ah okay! Yeah that's because the element no longer exists. So when using `driver.findElement(element)` it will throw an exception if it can't find the element. What you could do is update this to use `driver.findElements(element)` instead. This will return a list of all the matching elements, one to start off with in your case, or return an empty list instead if none are found. You can then use the length of this list to determine if it's displayed etc. Let me know if that helps. – Jason Anderson May 15 '18 at 14:50
  • I've amended my answer with a code example for how it would essentially work. – Jason Anderson May 15 '18 at 14:59
  • updated the code and the build solution displayed 2 errors for the below lines driver.FindElements(By.XPath("//*[contains(@id,'GetStatus')]" Any() – Hargovind May 15 '18 at 15:20
  • Yeah .Any() is just an example. driver.FindElements() will return either an empty list or a list of elements. So .Any() will need to be something like .Count, .Length etc. Essentially any way that you want to determine if the list contains any elements. – Jason Anderson May 15 '18 at 15:43
  • if i type elements. then i get only the below mentioned extensions compareto, equals, gethashcode, gettype, gettypecode, tostring i don't see anything like count or any as the extensions. – Hargovind May 15 '18 at 15:55
  • Updated the code to use `List elements` instead of `var elements `. Failing that you could also use `ReadOnlyCollection elements ` Sorry I am not currently in front of a PC so can't test the actual syntax, but it's along those lines. – Jason Anderson May 15 '18 at 16:16
  • Really appreciate your patience. have done the necessary changes and the build solution gave 1 error for the below mentioned line driver.FindElements(By.XPath("//*[contains(@id,'GetStatus')]"))); and below is error message "CS1503 Argument 1: cannot convert from'System.Collections.ObjectModel.ReadOnlyCollection' to 'string'" – Hargovind May 15 '18 at 16:29
  • That's okay no worries. Sorry I didn't notice before that your code is using `elements = wrapper.IsElementDisplayed( driver.FindElements(By.XPath("//*[contains(@id,'GetStatus')]")));` If you use the following without the wrapper it will return a ReadOnlyCollection of IWebElements. `var elements = driver.FindElements(By.XPath("//*[contains(@id,'GetStatus')]"));` I have updated the code, give that a go and let me know how you get on. And then from that you can use .Any(), .Count etc. – Jason Anderson May 15 '18 at 16:56
  • Excellent! Glad it helped – Jason Anderson May 16 '18 at 06:43
0

I think problem might be here, especially when you check if isDisplayed == true then this line buttonName_GetStatus = getnameofbutton1.Split('_'); overrides array so that infinitive loop appeared.

diazolin88
  • 125
  • 2
  • 10