机械手框架-在WebTable中搜索文本

时间:2020-11-05 14:40:24

标签: robotframework

下面是cssselector和xpath

#ctl00_MasterContentPlaceHolder_radGridSystemRoles_ctl00__0 > td:nth-child(3)
//*[@id="ctl00_MasterContentPlaceHolder_radGridSystemRoles_ctl00__0"]/td[3]

这是WebTable的一部分,我想在表中搜索文本

增量在下面

#ctl00_MasterContentPlaceHolder_radGridSystemRoles_ctl00__1 > td:nth-child(3)
//*[@id="ctl00_MasterContentPlaceHolder_radGridSystemRoles_ctl00__1"]/td[3]

#ctl00_MasterContentPlaceHolder_radGridSystemRoles_ctl00__2 > td:nth-child(3)
//*[@id="ctl00_MasterContentPlaceHolder_radGridSystemRoles_ctl00__2"]/td[3]

如何搜索表中的每一行以查找文本?

1 个答案:

答案 0 :(得分:0)

XPath只是一个字符串,因此您可以对其进行自定义以获取特定的行。

例如:

*** Settings ***
Library    SeleniumLibrary


Test Teardown  Close Browser

*** Variables ***
${BROWSER}           Chrome
${URL}               https://www.rottentomatoes.com/top/bestofrt/
${TEXT TO SEARCH}    The Irishman (2019)
${NUMBER OF COLUMN}  ${3}
${NUMBER OF ROWS}    ${15}


*** Test Cases ***
Test search in table
    Open Browser  url=${URL}
    ...           browser=${BROWSER}
    ${found flag}=  Set variable  ${False}
    FOR  ${row number}  IN RANGE  1  ${NUMBER OF ROWS}
    ${field locator}=  Get field XPath  ${row number}
    Log  ${field locator}
    ${field text}=  Get text  ${field locator}
    Log  ${field text}
    ${found flag}=  Set variable if  '${field text}'=='${TEXT TO SEARCH}'
    ...                              ${True}
    ...                              ${found flag}
    END
    Should be true  ${found flag}
    ...             msg=Movie '${TEXT TO SEARCH}' is not found in the table
    Close Browser

*** Keywords ***
Get field XPath
    [Arguments]  ${row number}
    ${xpath}=    Set variable  //table[@class='table']//tbody/tr[${row_number}]/td[${NUMBER OF COLUMN}]
    [Return]     ${xpath}

这里,我为表的特定字段//table[@class='table']//tbody/tr[${row_number}]/td[${NUMBER OF COLUMN}]使用了XPath模板,其中${row_number}是从for循环中获取的行号。

似乎对您的情况有用。