Robot Framework - 如何定位頁面元素

在做自動化測試的過程中,不管時web測試還是mobile測試,都會涉及到找元素。快速準確的找到我們需要的元素,既是自動化測試基本能力,也能提高我們工作的效率。

 

以下是部分找元素時常用的知識點,留着備用。

使用xpath進行元素定位

1. 當前節點之後的所有兄弟節點,使用following-sibling

例如://label[text()='Challenge1']/ancestor::table[@role="presentation"]/followingsibling::table//label[text()='Password']

2. 當前節點之前的所有兄弟節點,使用preceding-following

例如://A/C/preceding-following::td[@class=’mmm’]

3. 使用contains和元素屬性來確定唯一的元素

例如: //div[@class='x-datepicker x-layer x-ltr x-datepicker-default x-unselectable x-border-box' and not(contains(@style,'display: none;')) ]//a[@buttontext='Today']

//div[contains(text(),'appName')]/ancestor::tr[@data-recordindex='appIndex']/td[3]/div

4. 獲取當前的時間戳,使用Get Current Date

例如:${time}    Get Current Date     result_format=%H%M%S

5. 當前工程的相對路徑使用${EXECDIR}

例如:${EXECDIR}${/}project${/}resources

6. 關閉當前的webdriver以及瀏覽器,可調用cmd命令實現

例如  OperatingSystem.Run    cmd /c taskkill /F /im iedriverserver.exe

OperatingSystem.Run    cmd /c taskkill /F /im iexplore.exe

7. 秒中檢查一次,共檢查一分鐘,直到獲得該元素此句才運行結束,使用 wait until keyword succeeds

例如:Wait Until Keyword Succeeds    30s    5s    Wait Until Page Contains Element    ${VersionUpdate_Title}

使用css進行元素定位

1. 使用id來定義元素

例如:css=#inputField

2. 使用class屬性來定義元素

例如 css=.classme

.x-box-target .x-menu-item:nth-child(2)

3. 使用Javascript來執行命令

例如:Execute Javascript    $("${Provisioning_Capable_Apps_Details_Css}")[0].click();

注:此時的元素必須事以css selector來表示

 

--- End of Document ---

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章