I’m using Selenium 2.0 web driver. My script fails occasionally whenever I try locating something in my page. It throws an exception:
Unable to locate element: {“method”:”id”,”selector”:”username”};
part of my code:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement userName = driver.findElement(By.id("username"));
userName.clear();
userName.sendKeys("admin");
It passes successfully sometimes with the same code. I don’t understand what’s happening.
Sometimes this happens because of the page is loading slowlier than you expected. I am doing workaround by applying my own wrapper helper. It looks like this:
And later in the code:
This approach will try to find it and if not found, sleep for 200 miliseconds and try again. If not found in 3 seconds (editable), it will crash (you will probably have to say in the method that it throws some Exception)
I apply it whenever I am not sure how fast the page will load…