繁体   English   中英

如何使用机器人框架/python/selenium 为新添加的 webtable 行设置增量 xapth?

[英]How to set incremental xapth for newly added rows of a webtable using robot framework/python/selenium?

我遇到了一个特定的要求:我有一个 web 表,每天都会添加新行。

我想在 Robot Framework-Python-Selenium-Eclipse-RedEditor Plugin 中编写一个自动化脚本。 现在,我能够获取表格的总行数并能够获取特定列的文本。

我的要求

我想 go 到特定的新添加行并检查特定文本。 由于我需要将其自动化,因此新添加的行 xpath 将是增量的,那么如何自动化呢?

 ${rows} =  get element count   xpath=//*[@id="tableData"]/tbody/tr
 log to console   Total Rows= ${rows}
 
 ${nameColumn}=  get text   xpath=//*[@id="tableData"]/tbody/tr[4]/td[9] 
 log to console   name is =  ${nameColumn}
   

如果要添加 5、6、7 行,则 xpath 将像增量一样

xpath=//*[@id="tableData"]/tbody/tr[5]/td[9]
xpath=//*[@id="tableData"]/tbody/tr[6]/td[9]

我想以每次在表中添加新(或)时的方式编写脚本,然后如果我只运行脚本,它将检查 xpath 是否有新添加的行和 go 增量直到最后一行并读取并返回其来自提到的列的数据。

谢谢 !

还有更多方法可以实现这一点,我将向您展示一种。

首先,您需要能够在某处存储当前的行数。 因此,一个明显的解决方案是将数字写入到磁盘的文件中。 为此,您需要OperatingSystem库。

*** Settings ***
Library    OperatingSystem  

让我们设置一个变量来存储文件的名称,这样它就不会在以后的任何地方进行硬编码:

*** Variables ***
${file_name}=    last_row.txt

接下来,您需要考虑第一次运行脚本时该文件可能不存在。 您不希望您的脚本在这种情况下失败:

*** Keywords ***
Touch If Does Not Exist
    ${status}=    Run Keyword And Return Status    Get File    ${file_name}
    Run Keyword If    ${status} == ${False}    Touch    ${file_name}  

同样,我正在使用OperatingSystem库。 此处查看文档。

现在,您可能需要准备另外两个关键字,一个用于引导文件中的最后一个行号,一个用于将最后一个行号保存到文件中:

Load From File
    Touch If Does Not Exist
    ${content}=    Get File    ${file_name}    
    @{lines}=    Split to lines    ${content}
    ${len}=    Get Length    ${lines}
    ${row_number}=    Run Keyword If    ${len} > 0    Get From List    ${lines}    0    ELSE    Set Variable    0    
    [Return]    ${row_number}   

Save Into File
    [Arguments]    ${last_row_number}
    ${last_row_number}=    Convert To String    ${last_row_number}
    Create File    ${file_name}    ${last_row_number}

请注意,我使用的是String库中的Split Into Lines关键字和Collections库中的Get From List关键字。 此处此处查看文档。 所以我需要更新导入:

*** Settings ***
Library    OperatingSystem    
Library    Collections 
Library    String 

最后,我简化了您的测试用例,因为我没有可以实际尝试的网页:

*** Test Cases *** 
Increment Over Rows
    ${start_row_number}=    Load From File
    ${total_rows}=    Evaluate    ${start_row_number} + 5
    FOR    ${i}    IN RANGE    ${start_row_number}    ${total_rows}
        Log    ${i}
    END
    Save Into File    ${total_rows}

您需要更改此行:

${total_rows}=    Evaluate    ${start_row_number} + 5

到您获得表中实际行数的行,因此也许您可以使用Selenium库中的关键字Get Element Count

只是用一个完整的工作示例来总结:

*** Settings ***
Library    OperatingSystem    
Library    Collections 
Library    String       

*** Variables ***
${file_name}=    last_row.txt

*** Keywords ***
Touch If Does Not Exist
    ${status}=    Run Keyword And Return Status    Get File    ${file_name}
    Run Keyword If    ${status} == ${False}    Touch    ${file_name}           

Load From File
    Touch If Does Not Exist
    ${content}=    Get File    ${file_name}    
    @{lines}=    Split to lines    ${content}
    ${len}=    Get Length    ${lines}
    ${row_number}=    Run Keyword If    ${len} > 0    Get From List    ${lines}    0    ELSE    Set Variable    0    
    [Return]    ${row_number}   

Save Into File
    [Arguments]    ${last_row_number}
    ${last_row_number}=    Convert To String    ${last_row_number}
    Create File    ${file_name}    ${last_row_number}

*** Test Cases *** 
Increment Over Rows
    ${start_row_number}=    Load From File
    ${total_rows}=    Evaluate    ${start_row_number} + 5
    FOR    ${i}    IN RANGE    ${start_row_number}    ${total_rows}
        Log    ${i}
    END
    Save Into File    ${total_rows}

当我第一次运行脚本时,将创建一个包含数字 5 的文件,再次执行后,该文件将包含 10,然后是 15,依此类推。 它应该可以正常工作。 我将让您了解有关您的实际 DOM 的实现细节。 我想最大的麻烦是找出如何在某处存储行数并在下次加载它,我在我的回答中展示了这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM