7

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


            }

        }

  }

}

7 Answers 7

11

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*/
Sign up to request clarification or add additional context in comments.

Comments

8

Not sure what your requirement is. But, couple of things to keep in mind.

  1. Selenium may find the element that meets same criteria but they are hidden
  2. Even if element is not hidden it may not be in a ready state to accept any interaction.

if you know for sure the element is not hidden then you can use the following wait for the element to be visible

 new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible("your selector"); 

1 Comment

can you please help me to convert this command to JS? I've the same issue but I'm using in Protractor on Node.JS. thanks @Saifur
2

For some browsers it happens that once mouse hover action is performed, but the menu list disappear quickly before Selenium identify the next sub menu item. In that case it is better to use perform() action on the main menu to hold the menu list till the time Selenium identify the sub menu item and click on it.

Here

WebElement xWL = driver.findElement(By.xpath("x path text"));

Actions xAct = new Actions(driver);

Instead of this:

xAct.moveToElement(xWL).build().perform();

Below code will resolve the "element not visible" issue

xAct.moveToElement(xWL);
xAct.click();
xAct.perform();

Comments

0

I guess you are trying to click on hyperlinks, if you are getting 'ElementNotVisibleException' this means that elements might be hidden. Does it take long time for the elements with locator 'a.uiv2-add-button.a2c' to render on the UI after you select a Category from the left panel? If yes than interaction wiith non visible elements will always throw 'ElementNotVisibleException'

Comments

0

I had a similar issue and the reason was, there were more hidden elements along with the actual element that i tried to find. So click was actually trying to interact with hidden element and throwed the exception.

Solution- I fine-tuned the element's xpath to make it unique which resulted the interaction to the actual element.

2 Comments

"Fine tuning the elements XPath" is a bit too abstract a solution to attract (many) upvotes. Could you edit and make it a bit more tangible, for instance code how you solved your related issue?
It solved my issue when there were more than an element with the same id.
0

To handle it , you can use explicit wait function in selenium to locate element. Most of the time it works.

Comments

-1

Just put the thread to sleep for some mil seconds

Thread.sleep(5000);
WebElement zoneName=driver.findElement(By.xpath("//*[@id=\"zoneName\"]"));
zoneName.sendKeys("kandy");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.