Click all pop-up items in the list
I have a list of all the elements on a page with a class name popup and I would like to open and close each element one at a time. Here is the code 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 get a System.NullReferenceException : Object reference not set to an instance of an object.
in the string IWebElement welcomePopup
. How do I get it to go through each item in the list? I am very new, so let me know if I am missing something that is very obvious to you.
+3
source to share
1 answer
It will help if you can provide the actual page.
Anyway, can you try this?
String parentWindow = driver.CurrentWindowHandle;
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();
driver.SwitchTo().Window(parentWindow);
}
First, you need to get the original window handle. Switch to Popup Instance Perform your actions in the popup window. Here try to return to your original window with
driver.SwitchTo().Window(parentWindow);
Hope it helps.
+1
source to share