2017-08-08 122 views
1

if條件在try塊不起作用。嘗試捕捉塊如果條件不工作在硒webdriver C#

即使元素(使用Xpath定義)出現/顯示在網頁中,腳本執行始終會從try catch循環中走出。 if條件失敗。

任何幫助將不勝感激。

try 
{ 
    string path = "//*@id='form_780_0']/div[2]/div/div[3]/div"; 
    if(driver.FindElement(By.XPath(path)).Displayed) 
    { 
     Assert.Fail("Fails"); 
    } 
} 
catch (Exception) 
{ 
    //There is nothing here 
} 

// Some code here and this is executed after evaluating if condition 
// even if element is displayed 
+0

異常告訴你什麼? –

+0

刪除嘗試和捕獲。讓它失敗併發布完整的例外 –

+0

@RufusL:我更新了我的問題。執行也不會進入異常路徑。在評估條件之後,它會退出try catch循環。 – user8356727

回答

0

「執行不進入異常路徑無論是。它超出嘗試捕捉循環的,如果條件評估後,」

  • 這意味着元素被發現,但它不顯示。如果沒有找到,你會得到一個NoSuchElementException異常。此外,如果元素被找到並顯示,您將觸發Assert.Fail方法