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(...)確立できませんでした。したがって、待機はタイムアウトで失敗します。

プログラムを続行したい場合は、wait を try-catch ブロックで囲み、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

あなたが持っているコードでは常にタイムアウトが発生します。これは問題のある行です:

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 に答える
0

ここで、2 つの解決策があります...

  1. WebDriverWait 時間を増やして、問題が解決するかどうかを確認してください。
  2. WebDriverWait 時間が唯一の原因であるため、firebug によって同じ要素が正常に検出された場合は、firebug によって同じ要素を見つけようとします。
于 2014-02-17T10:40:21.007 に答える