Daniel Shmayovich
Daniel Shmayovich

Reputation: 9

C# selenium web driver how do while loop on element

This is my code in C# I press the delete button and open a pop up where I select the amount of deletions that I want to delete and press again on delete it will delete. Perhaps it is more correct to ask how I do while this delete element

PropertiesCollections.driver.FindElement (By.LinkText ("Delete")). Click ();

As long as it appear true that it will perform the deletion steps and if it does not continue the test

PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click();
new SelectElement(PropertiesCollections.driver.FindElement(By.Id("remove_shares"))).SelectByText("1");
PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click();

I want to make a loop if the delete button appear, it will do all the steps of the deletion, and if it does not continue for the other test


im try with this code

   var links =   PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click();
             while (links = true)
              {
               PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click();
               PropertiesCollections.driver.FindElement(By.Id("remove_shares"));
               PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click();
             }

but i get error Error 1 Cannot assign void to an implicitly-typed local variable

Upvotes: 0

Views: 1498

Answers (1)

JeffC
JeffC

Reputation: 25542

The first line of code is assigning the return of .Click() to the variable links but .Click() returns void (nothing).

The logic for what you want to do is:

  1. Check to see if the Delete link exists
  2. If it does, click on it
  3. Do stuff
  4. Repeat 1-3

IReadOnlyCollection<IWebElement> links = PropertiesCollections.driver.FindElements(By.LinkText("Delete")); // gets a collection of elements with Delete as a link
while (links.Any()) // if the collection is not empty, this evaluates to `true`
{
    links.ElementAt(0).Click(); // click the first (and probably only?) element
    // do stuff
    PropertiesCollections.driver.FindElement(By.Id("remove_shares"));
    PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click();
    // get the Delete links again so we can return to the start of the `while` and see if it's still not empty
    links = PropertiesCollections.driver.FindElements(By.LinkText("Delete"));
}

Upvotes: 1

Related Questions