2016-08-11 52 views
2

我想運行一個關鍵字,如果頁面上存在一個元素。機器人框架:如何使等待,直到硒庫的關鍵字返回true或false

我嘗試使用Selenium庫的Wait Until Page Contains Element關鍵字,但無論元素是否存在,它都將返回「None」。我嘗試設置自定義錯誤,但不會工作,要麼:

${condition} =  Wait Until Page Contains Element ${locator} timeout=5 error=false 
Run Keyword if '${condition}'=='false'  click element ${some_refreshButton_locator} 

關鍵字時,我提出條件點擊元素$ {}定位器將只運行「$ {}條件」 ==「無」

我做錯了什麼,我怎麼能做一個硒庫等待...關鍵字返回true或false。

謝謝!

回答

4

等到頁面包含元素不會返回任何內容,但會在元素未找到時引發錯誤。一種解決方法是圍繞它運行運行關鍵字並忽略錯誤

*** Settings *** 
Library Selenium2Library 

*** Test Cases *** 
Test wait 
    Open Browser  http://www.google.com/ gc 
    ${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Stackoverflow timeout=2 error=false 
    Run Keyword if '${condition}'=='false'  Log clicking 
    Close All Browsers 
+0

佩卡嗨,這偉大工程與不存在的頁面上的元素,但我的元素是樹狀視圖中的複選框,它是現有的,但不可見。使用上面的代碼片段會拋出「ElementNotVisibleException:Message:Element當前不可見,因此可能不會與之交互」如何捕獲它並在拋出時運行關鍵字? – emi

+0

嗨再次Pekka,我是一個機器人新手,仍在學習。用關鍵字「等待直到元素可見」對相同的概念進行了嘗試,現在它對我來說非常合適。非常感謝! – emi

3

「運行關鍵字和返回狀態」也可以用來獲取如下真/假狀態:

${condition} =  Run keyword And Return Status Wait Until Page Contains Element ${locator} timeout=5 error=false 
相關問題