2017-04-13 35 views
0

我正在尋找一個元素,如果元素不存在,我想給一個體面的錯誤消息說「元素不存在」;但它來自塊和投擲消息像xpath找不到的元素。量角器 - 嘗試捕捉 - 錯誤處理

下面是我嘗試過的代碼,但仍然收到相同的錯誤。 例如a =量角器,如果值存在,則表示文字佔優勢 - 量角器;如果是在情況不存在,而不是說元素不存在/錯誤時它說:「沒有的元素中找到element(by.xpath("//div[@title='"protractor"']"))其次是大的錯誤消息。如何解決這個

this.gridverify = function (a) { 
 
     browser.sleep(10000); 
 
     try {    
 
      var elm = element(by.xpath("//div[@title='" + a + "']")); 
 
      if (elm.isPresent) { 
 
       elm.getText().then(function (x) { 
 
        console.log("text Prevails: " + x); 
 
       }) 
 
      } 
 
      else {     
 
       console.log('element not present'); 
 
      } 
 
     } 
 
     catch (err) { 
 
      console.log('error occured'); 
 
     } 
 
    }

+0

看看你的xpath'// div [@title =''量角器'']'。它應該是'// div [@ title ='protractor']'。你在找我嗎? –

+0

@KishanPatel,感謝您的關注。當我複製粘貼,它發生了;這不是問題。我已經通過了正確的。 – kavitha

+0

您手動進入該頁面並嘗試使用相同的'xpath'進行檢查並查看是否找到元素。 '// div [@title =''量角器'']' –

回答

0

考慮挖掘更深入的承諾。您的代碼有很多錯誤的。http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/promise.html

this.gridverify = function (a) { 
    browser.sleep(10000); // Why this needed? Can you update to .wait() ? 
    var elm = element(by.xpath("//div[@title='" + a + "']")); 
    elm.isPresent().then(present => { 
     if (present) { 
      elm.getText().then(function (x) { 
       console.log("text Prevails: " + x); 
      }) 
     } else { 
      console.log('element not present'); 
     } 
    }, err => { 
     console.log('error occured', err); 
    }) 
} 

你的try/catch不會爲你在異步代碼預期工作。

+0

謝謝你檢查...將進入鏈接,並嘗試 – kavitha