I am trying to loop through a drop-down menu, I am able to get each and every listed element but when I use the .click()
and .submit()
commands, it selects the first one and does not continue any further. I am aware for it to continue any further, I need to reselect the drop-down arrow so the list becomes visible to selenium after every submit. I tried commenting to make it readable. ANY help is greatly appreciated.
I wrote down the following,
WebElement search = driver.findElement(By.id("search-form"));
WebElement arrowDownButton = search.findElement(By.className("dropdown-toggle"));
arrowDownButton.click();
WebElement menu = search.findElement(By.className("dropdown-menu"));
List <WebElement> listOptions = menu.findElements(By.tagName(LI)); //selecting the listed elements , LI is "li" (declared previously)
int numberOfCountries = listOptions.size();
log("We have " + numberOfCountries + " entires");
int i=0; //for looping
//store into an array because web elements disappear
WebElement []listOfCountries = new WebElement[listOptions.size()]; //making an array of size listed elements
for (WebElement aOption : listOptions)
{
listOfCountries[i] = aOption; //saving the value into an array
String dataValue = aOption.getAttribute("data-value"); // what country am I wanting click
i++;
}
for(WebElement country : listOfCountries)
{
log(country.getText()); //log is a another function executing System.out.println
country.click(); //clicking on the web element
country.submit(); // submitting the element
arrowDownButton.click(); //reselecting the drop-down menu -> Why isn't this working?
}
}
Here are some methods I have already tried: Instead of creating, saving and iterating through an array, I just directly tried to click the element. However, it also just clicks the first element.
Here is the error I am receiving: Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with arrowDownButton.click();
but I cannot figure out WHY it fails to RESELECT.
for(WebElement country : listOfCountries)
{
log(country.getText()); //log is a another function executing System.out.println
country.click(); //clicking on the web element
Thread.Sleep(500);
country.submit(); // submitting the element
// You have to reselect this element. Because doesn't make sense anymore.
// Try reselect next element
arrowDownButton.click(); //reselecting the drop-down menu -> Why isn't this working?
}