0
package wait1;

import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Explicit {

    public static void main(String[] args) {
        FirefoxDriver driver= new FirefoxDriver();

        WebDriverWait wait= new WebDriverWait(driver,20 );

        driver.get("http://www.91mobiles.com/");
        driver.findElement(By.xpath("//*[@id='q']")).sendKeys("Micromax");

        //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='ui-id-22']/span[2]")));

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id=ui-id-172")));
        driver.findElement(By.xpath("//*[@id='ui-id-52']")).click();

    }

}

I am getting the following error when I execute the above script:

    Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for visibility of element located by By.id: id=ui-id-172
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.FluentWait.timeoutException(FluentWait.java:259)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
    at wait1.Explicit.main(Explicit.java:20)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"id=ui-id-172"}
Command duration or timeout: 21 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Session ID: fd977506-2457-4981-a304-f9a9b6b57f4e
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=17.0.7}]
    at sun.reflect.GeneratedConstructorAccessor10.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:348)
    at org.openqa.selenium.By$ById.findElement(By.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:522)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:520)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:130)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
    ... 1 more
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"id","selector":"id=ui-id-172"}
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: driver.version: unknown
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
user2000385
  • 1
  • 1
  • 1
  • 1

4 Answers4

2

The behavior you are getting is the expected bahavior. You specified a 20 second explicit wait and in that time the ExpectedConditions.visibilityOfElementLocated(...) could not be established. So the wait fails with a timeout.

If you want to go on in your program you need to surround the wait in a try-catch block and catch org.openqa.selenium.TimeoutException

luksch
  • 11,497
  • 6
  • 38
  • 53
  • Actually XPATH is Varying each time, that's why its throwing TimeoutException. What should we do in this case? – user2000385 Jul 31 '13 at 04:04
  • I am expecting to click on Micromax Canvas HD A116. – user2000385 Jul 31 '13 at 04:07
  • I can't know what you should do. But from what I understand, you actually do expect the element to be visible in every case. So it is a good thing that you get this exception, because it is clearly not there for clicking. Maybe you need o wait even longer? I really can't know without more background info. – luksch Jul 31 '13 at 07:15
1

Try using like this

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='ui-id-52']"));

Make sure that whatever identification type you use while waiting should be in the statement you use for action. (e.g. click, set etc.)

Brant Olsen
  • 5,628
  • 5
  • 36
  • 53
0

Here, we have two solutions...

  1. Try increasing your WebDriverWait time to check if it fix the issue.
  2. Try to locate same element by firebug, if it is successfully found by firebug, than WebDriverWait time is the only culprit.
0

You'll always get a timeout with the code you have. This is the offending line:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id=ui-id-172")));

Note how you have By.id("id=ui-id-172"). This is wrong. It would look for an object that has the id id=ui-id-172. While it is possible to have an id with this value (yes, I tried and it works) it is unlikely that this is what you want, especially given your XPath expression on the next line. What you want, to be consistent with the rest of your code, is By.id("ui-id-172").

Louis
  • 146,715
  • 28
  • 274
  • 320