Click all popup elements in list

Viewed 399

I have a list of the all elements on a page with a class name of popup, and I would like to open and close each element one at a time. Here is the code that I have so far:

IList<IWebElement> links = driver.FindElements(By.ClassName("popup"));

for (int i = 0; i < links.Count; i++)
{                                               
     IWebElement welcomePopup = driver.FindElement(By.XPath(string.Format("//div[@id='Buttons']/table/tbody/tr/td/table/tbody/tr/td[contains(text(),'" + links[i].Text + "')]")));
     PopupWindowFinder popupFinder = new PopupWindowFinder(driver);
     string welcomePopupHandle = popupFinder.Click(welcomePopup);

     if (!string.IsNullOrEmpty(links[i].Text))
     driver.SwitchTo().Window(welcomePopupHandle);
     driver.FindElement(By.Id("cmdClose")).Click();                
}

This only opens and closes the first element; once the first element is closed I receive a System.NullReferenceException : Object reference not set to an instance of an object. on the IWebElement welcomePopup line. How do I get it to go through each item in the list? I am very much a beginner, so let me know if I am missing something that is very obvious to you.

1 Answers
Related