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();

    }

}

执行上述脚本时出现以下错误:

    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
4

4 回答 4

2

您得到的行为是预期的行为。您指定了 20 秒的显式等待,但在此期间ExpectedConditions.visibilityOfElementLocated(...)无法建立。所以等待失败并超时。

如果你想在你的程序中继续,你需要将等待包围在一个 try-catch 块中并捕获org.openqa.selenium.TimeoutException

于 2013-07-30T13:25:48.390 回答
1

尝试像这样使用

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

确保您在等待时使用的任何标识类型都应包含在您用于操作的语句中。(例如点击、设置等)

于 2013-08-08T15:00:07.470 回答
0

在这里,我们有两个解决方案...

  1. 尝试增加您的 WebDriverWait 时间以检查它是否解决了问题。
  2. 尝试通过firebug定位相同的元素,如果它被firebug成功找到,那么WebDriverWait时间是唯一的罪魁祸首。
于 2014-02-17T10:40:21.007 回答
0

您拥有的代码总是会超时。这是违规行:

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

请注意您如何拥有By.id("id=ui-id-172"). 这是错误的。它会寻找一个具有 id 的对象id=ui-id-172。虽然可以有一个具有此值的 id(是的,我尝试过并且它有效)但这不太可能是您想要的,尤其是考虑到下一行的 XPath 表达式。您想要与您的其余代码保持一致的是By.id("ui-id-172").

于 2015-04-09T16:24:57.530 回答