Shiva krishna Chippa Shiva krishna Chippa - 4 months ago 57
Java Question

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

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");
}


}

}

}

}

Answer

Finally this worked for me. Element is not currently visible and so may not be interacted with.

Initially it was like test was successful only 2 of 5 times. Was not sure how it was working sometimes and others not.

Worked by reducing the security settings in IE. Enable all activeX controls. Enable scripts and IFRAMES also. Some of these will warn to put computer at risk, but it was the only solution I had. Introduce Explicit wait by using presenceOfElementLocated instead of visibilityOfElementLocated at every point where page load takes time.

    WebDriverWait wait = new WebDriverWait(driver,30);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='campaignListTable']")));   /*examining the xpath for a search     
    box*/
    driver.findElement(By.xpath("//*[@id='campaignListTable']")).sendKeys("TEXT");   /*enter text in search 
    box*/
Comments