org.openqa.Selenium.Elementnotvisibleexception: Element is not Currently Visible and so may not be interacted with Command Duration or timeout

Asked

Viewed 1,106 times

1

This exception happens when the some tests run on Amboo. The strange thing is when I run locally it works and are random tests that break. Someone’s been through it?

The Exception is this:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 30.11 seconds Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40' System info: host: 'N/A', ip: 'N/A', os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-52-virtual', java.version: '1.7.0_72' Session ID: 126f178d-2ac6-4c4f-a939-09e21052c04f Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=34.0}]
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.11 seconds
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'N/A', ip: 'N/A', os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-52-virtual', java.version: '1.7.0_72'
Session ID: 126f178d-2ac6-4c4f-a939-09e21052c04f
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=34.0}]
(48 more lines...)

This is an example of one of the codes in which this problem occurs:

String name;
String key;

@Before
public void setUp() throws InterruptedException {

    driver.findElement(By.id("menu_topMenu.maintenance")).click();
    Thread.sleep(1000);
    driver.findElement(By.id("menu_topMenu.route")).click();
}

@Test
public void test() throws InterruptedException {

    AddRoute addRoute = new AddRoute(driver);
    addRoute.add();
    name = addRoute.getName();
    key = addRoute.getKey();
    Thread.sleep(3000);
    driver.findElement(By.cssSelector
            ("#Route_btn_novo")).click();
    driver.findElement(By.cssSelector
            ("#Route_key > input[type=\"text\"]")).sendKeys(key);
    driver.findElement(By.cssSelector
            ("#Route_description > input[type=\"text\"]")).clear();
    driver.findElement(By.cssSelector
            ("#Route_description > input[type=\"text\"]")).sendKeys(name);
    driver.findElement(By.cssSelector
            ("#Route_organization > div.lookUpHitArea")).click();
    driver.findElement(By.cssSelector
            ("#MonetUIFieldsLookupGridContainer_organization > div.slick-pane.slick-pane-top.slick-pane-left > div.slick-viewport.slick-viewport-top.slick-viewport-left > div > div:nth-child(2) > div.slick-cell.l0.r0.cell-row")).click();
    driver.findElement(By.cssSelector
            ("#Route_plannedCost > input")).sendKeys("10000");
    driver.findElement(By.cssSelector
            ("#Route_origin > div.lookUpHitArea")).click();
    driver.findElement(By.cssSelector
            ("#MonetUIFieldsLookupGridContainer_location > div.slick-pane.slick-pane-top.slick-pane-left > div.slick-viewport.slick-viewport-top.slick-viewport-left > div > div:nth-child(1) > div.slick-cell.l1.r1.cell-row.selected")).click();
    driver.findElement(By.cssSelector
            ("#Route_lastStopIsDestination > input")).click();
    Thread.sleep(1000);
    driver.findElement(By.cssSelector
            ("#Route_btn_save_and_close")).click();
    Thread.sleep(3000);
    assertTrue("Rota existente nao validada corretamente",
            isDisplayedByCssSelector(driver, "#form_and_grid > div > div.monetInlineAlerContainer > div"));
}

}

  • Hello, could you please put the source code?

  • @Paulohdsousa, I put an example

2 answers

0

This problem is due to the element not being loaded yet, Selenium is very fast and sometimes the command goes through the screen without it being fully rendered. In local testing, loading is usually faster.

Try to use this code:

public static void waitForPageToLoad(WebDriver driver) {          

  while( !((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete") ){
     try{
         Thread.sleep(500) ;
     } 
     catch (InterruptedException e) { 
          e.printStackTrace();
     }
  }        
    try {
         Thread.sleep(2000); 
    } 
    catch (InterruptedException e) { 
       e.printStackTrace();
  }        
}

Or simply the command below to force a wait on Selenium execution, where it is by informing the time in milliseconds that the thread will wait until the element loads.

Thread.sleep(500);
  • always use Thread.Sleep() but still this occurs... and I know that thread.Sleep() is not the best option...

0


In my experiments with Selenium I also got random errors, including with the exception ElementNotVisibleException.

In general this was due to the way the Web Driver interacts with the browser, remembering that in current versions the Selenium API actually runs on a specific Browser Driver (FirefoxDriver, in this case).

The Web Driver tries to simulate the actions of a user, but almost always there are differences in relation to the actual use, either for reasons of positioning elements that are calculated incorrectly or by the difference of timing typing or mouse movements.

A specific case I remember occurred on a menu with several sub-levels. While a real user could navigate through the menu without problems, the events generated by the Webdriver could not properly affect the submenus because Selenium incorrectly calculated the position of the items.

In such cases, the solution found was to replace native calls to the Selenium API with Javascript commands that executed the desired actions.

  • I don’t know if it’s directly related to mouse movements... Can you take a look at the code I added?

  • @Sandymaciel The question of mouse movement was just one example. In fact, the problems are directly related to the failure in the element positioning calculation or "parents" of the element that are have CSS styles that affect their positions, or to Javascript events that hide/display or create elements on the screen. I saw you have some sleeps in your code. This is to wait for some event?

  • Yes, it’s waiting for events to appear on the screen... for some reason I can’t get Wait’s to work... tips?

  • @Sandymaciel So the problem may occur in cases where Selenium tries to interact with the element, but it is not yet in a visible state and ready to use. The tip would be to wait for a condition that would confirm the completion of such events. This is not always easy, sometimes being necessary to put some flag within the system itself for this. When there are Ajax requests being made by the system, for example through the function $.ajax jQuery, it is possible to inspect the request queue. In the case of transitions or animations, you could look at some attribute to see already finished.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.