I am trying to execute below Selenium Web driver script, But I am getting org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with error several time(not all the times). sometimes in loops first iteration and sometimes in 2 iteration and sometimes without starting loop. It printing all the available items count correctly but whey trying to click on items, it showing Element is not currently visible...
public void pickitems() throws Exception
{
Webdriver driver = new firefoxdriver();
driver.get("http://www.bigbasket.com");
driver.manage().window().maximize();
//Selecting Location
List<WebElement> list = driver.findElement(By.id("ftv-city-popup")).findElements(By.tagName("button"));
int location = r.nextInt(list.size());
list.get(location).click();
//Selection random Category from left panel through list
Thread.sleep(30000);
List<WebElement> xyz = driver.findElement(By.id("uiv2-main-menu")).findElements(By.className("top-category"));
System.out.println(xyz.size());
Random r = new Random();
int category = r.nextInt(xyz.size());
xyz.get(category).click();
for (int i = 0; i < 3; i++) {
Thread.sleep(30000);
List<WebElement> availableItems = driver.findElements(By.cssSelector("a.uiv2-add-button.a2c"));
System.out.println(availableItems.size());
if (availableItems.size() > 0)
{
int selectItem = r.nextInt(availableItems.size());
availableItems.get(selectItem).click();
}
else
{
Thread.sleep(30000);
List<WebElement> availableItems2 = driver.findElements(By.cssSelector("a.uiv2-add-button.a2c"));
if (availableItems2.size() == 0) {
System.out.println("No more items are available. Sorry for the inconvenience");
}
else {
Assert.fail("Unable to select items. May be page loading issue");
}
}
}
}
}