0
String parentHandle = driver.getWindowHandle();     
driver.findElement(By.id("ImageButton5")).click();
for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle);                
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);                    
}
driver.findElement(By.id("txtEnterDescription")).sendKeys("Test");
driver.findElement(By.id("chklstAllprocedure_0")).click();

このコードを使用したところ、エラーが発生しました

「スレッド "main" org.openqa.selenium.NoSuchElementException での例外: ID == txtEnterDescription の要素が見つかりません (警告: サーバーはスタックトレース情報を提供しませんでした) コマンド期間またはタイムアウト: 30.05 秒". このテキスト ボックスの HTML コードは "" です。

これから私を助けてください

4

3 に答える 3

2

確かに2つのケースでのみ「NoSuchElementException」に直面する可能性があります。

 1.The Element is yet to be Loaded
     - Have an appropriate wait logic here.
 2. You may try to locate the element wrongly .
     - Double Check your Xpath/ID(what ever)
     - Make sure you are in the same frame where the element is present.If not, switch to the frame then.
于 2013-10-03T04:49:25.127 に答える
0

正しいウィンドウに切り替えていることを確認してください

理由 1 : 正しいウィンドウに切り替えていることを確認してください

以下に示すように、必要なウィンドウに切り替えるユーティリティメソッドがあります

public class Utility 
{
    public static WebDriver getHandleToWindow(String title){

        //parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
        WebDriver popup = null;
        Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
        System.err.println("No of windows :  " + windowIterator.size());
        for (String s : windowIterator) {
          String windowHandle = s; 
          popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
          System.out.println("Window Title : " + popup.getTitle());
          System.out.println("Window Url : " + popup.getCurrentUrl());
          if (popup.getTitle().equals(title) ){
              System.out.println("Selected Window Title : " + popup.getTitle());
              return popup;
          }

        }
                System.out.println("Window Title :" + popup.getTitle());
                System.out.println();
            return popup;
        }
}

ウィンドウのタイトルがパラメーターとして渡されると、目的のウィンドウに移動します。あなたの場合、あなたはすることができます。

Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");

同じ方法を使用して親ウィンドウに再度切り替えます

Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");

この方法は、複数のウィンドウを扱う場合に効果的です

Resaon 2 : 要素を待つ

WebdriverWait wait = new  WebdriverWait(driver,7000);
wait.until(ExpectedConditions.visbilityOfElementLocatedBy(By.name("nameofElement")));

理由 3 : 要素がフレーム内にあるかどうかを確認する前のフレームに切り替える場合

driver.switchTo.frame("frameName");

それが機能するかどうかを教えてください..

于 2013-10-03T06:47:56.483 に答える