Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


            }

        }

  }

}
like image 583
Shiva krishna Chippa Avatar asked Sep 18 '14 15:09

Shiva krishna Chippa


People also ask

How do I fix ElementNotVisibleException?

First Solution: Try to write unique XPATH that matches with a single element only. Second Solution: Use Explicit wait feature of Selenium and wait till the element is not visible. Once it is visible then you can perform your operations.

What do you do if the WebElement is not visible?

Element not visible exception can be resolved by using Explicit wait. Explicit wait in selenium will wait until the element is visible. Once it's visible you can perform the necessary operation. WebDriverWait wait = new WebDriverWait(driver,30); WebElement e = wait.

How do you handle invisible hidden elements in Selenium?

Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page. Selenium runs the Javascript commands with the executeScript method. The commands to be run are passed as arguments to the method.

What is org Openqa Selenium?

Describes a series of key/value pairs that encapsulate aspects of a browser. ContextAware. Some implementations of WebDriver, notably those that support native testing, need the ability to switch between the native and web-based contexts. Credentials.


1 Answers

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*/
like image 63
Akshay Avatar answered Oct 27 '22 01:10

Akshay