4

私は自動化テストにまったく慣れていません。いくつかのチュートリアルを参照した後、自動テストケースを作成しました。自動化しようとしているテストケースは、テーブルのヘッダーの1つをクリックした後、並べ替えが正しく機能したかどうかを確認することです。

私の自動テストケースは、次の例外を除いて失敗します。

org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
Command duration or timeout: 12 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:08:56'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.35-30-generic',     java.version: '1.6.0_20'
Driver info: driver.version: RemoteWebDriver
Session ID: 95b80ea0-26ac-45e0-a407-79f5b687504a
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at  sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:244)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:169)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:240)
at org.openqa.selenium.By$ByTagName.findElements(By.java:312)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:151)
at Sorting.sorting_column(Sorting.java:68)

コードは次のとおりです。

        //Fetching values from the column of a table
    WebElement table = driver.findElement(By.id("dnn_ctr381_View_DealerList_ctl00"));
    List<WebElement> rows = table.findElements(By.tagName("tr"));

    for (WebElement row : rows) {

        List<WebElement> cells = row.findElements(By.tagName("td"));
        if (!cells.isEmpty() && cells.get(0).isDisplayed()) {
            a = cells.get(0).getText();
        }
        b[i] = a;
        i++;
    }
    //Click on column header to sort   
    driver.findElement(By.cssSelector("html body#Body form#Form div#container.container div#Wrapper div#Main div#Panes div#ContentsContainer.box-shadow div#Contents div#Content div#dnn_ContentPane.MainContent div.DnnModule div.dnnPEMContNotitle div#dnn_ctr381_ContentPane.dnnPEMContNotitleBody div#dnn_ctr381_ModuleContent.DNNModuleContent div#dnn_ctr381_View_dnn_ctr381_View_DealerListPanel div#dnn_ctr381_View_DealerList.RadGrid table#dnn_ctr381_View_DealerList_ctl00.rgMasterTable thead tr th.rgHeader a")).click();
    WebElement table1 = driver.findElement(By.id("dnn_ctr381_View_DealerList_ctl00"));
    List<WebElement> rows1 = table1.findElements(By.tagName("tr"));
    for (WebElement row1 : rows1) {
        List<WebElement> cells1 = row1.findElements(By.tagName("td"));// Exception here
        if (!cells1.isEmpty() && cells1.get(0).isDisplayed()) 
            {

         c = cells1.get(0).getText();

       }

例外は次の行からです:

List<WebElement> cells = row1.findElements(By.tagName("td"));

誰かがこの問題の原因とそれを解決する方法を教えてもらえますか?

どんな助けでも大歓迎です

4

4 に答える 4

9

StaleElementExceptionクライアントコードで(によってfindElement)参照したDOM要素がもう存在しないことを通知するためにあります。あなたの場合、そのrow1要素はもう存在しません。クリックして並べ替えるのがすぐ上にあり、並べ替えが確実に終了するように注意が払われていないため、これは確かにタイミングの問題です。ソートがどのように実装されているか、またはどのくらいの時間がかかるかについての洞察がなければ、それは私の最善の推測です。DOMオブジェクトを反復処理しているときに並べ替えが終了すると、これらはDOMで再配置され、クライアントコードで失われます。

于 2013-01-03T09:54:00.760 に答える
4

まあ、StaleElement例外は本当にイライラする可能性があります..特にChromeドライバーで作業している間...しかし今のところ回避する唯一の方法は私がRaw待機と呼んでいるものを使用することです:

try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

純粋主義者は私たちがマジックナンバーを使用していると言うかもしれませんが、安全な待機の大きさを知ったら、95%以上の場合に効果的であることがわかりました。

于 2013-11-19T11:43:52.283 に答える
2

を使用webDriverwaitして、要素がdomに配置されるまでドライバーを待機させます。

WebDriverWait wait = new WebDriverWait(driver,20);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*

[@id='mndf']/form/fieldset/table/thead/tr/th")));

要素が配置されているかどうかに関係なく、出力ステートメントを使用します。

System.out.println("element located");

これがお役に立てば幸いです。

于 2014-07-11T14:32:33.743 に答える
0

これは、ページでインタラクションが発生したときに一部のJavaScriptが実行されるため、DOMが変更されることと関係があります。エラーが要素が古くなったことを示しているので、解決策はそれを取り除くためにdriver.findElementをもう一度実行することです。同じ問題に直面し、それを解決しました

于 2015-05-14T05:25:09.203 に答える