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 ---

 

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