2012-01-24 5 views
1

私はorg.openqa.selenium.NoSuchElementExceptionをキャッチする機能をしようと、ここでスカラ座での私の実装でいます:セレンのisDisplayed()からNoSuchElementExceptionをキャッチしますか?

def doesElementExists() = {  
    try { 
    //as long as isDisplayed() returns a boolean value, it means the element exists in the html code 
    seleniumElement.isDisplayed() 
    true 
    } catch { 
    case element_not_found_exception: org.openqa.selenium.NoSuchElementException => { 
     false 
    } 
    } 
} 

しかし、できるだけ早く私は、私が期待している、存在しない要素をチェックして偽を返す関数は、catchブロックが例外を処理しませんでした、なぜ私が疑問に思って吹き飛ばすとバック​​

org.openqa.selenium.NoSuchElementException, 
: Unable to locate element: {"method":"id","selector":"nonexistent-element"}; 

を投げますか?

答えて

0

IsDisplayedはすでに見つかっている要素のプロパティです。あなたがやっていることは、本質的に、この(C#コード)である:

var element = driver.FindById("nonexistent-element"); //<---This is what throws NoSuchElementException 
try 
{ 
    var displayed = element.IsDisplayed; 
} 
catch(NoSuchElementException) 
{ 
} 

だから、あなたが望むものを達成するために、あなたのdoesElementExists関数は、パラメータとして、ロケータに取り、発見周りのtry-catchステートメントをラップする必要があります素子。

関連する問題