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

问题描述 投票:0回答:1

我遇到了一些不同的要求。有一个网络表,每天都会在其中添加新行。我想使用机器人框架-Python-Selenium-Eclipse-RedEditor插件编写一个自动化脚本。到目前为止,我已经能够获取表的总行数并能够获取particuler列的文本。我的要求:我要继续新添加的特定行,并检查particuler文本。由于我需要对其进行自动化,因此新添加的行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]

我想编写脚本的方式是每次在表中添加新的rowrows时,如果我只运行脚本,它将检查xpath中是否有新添加的行并继续执行直到最后一行,并从提到的列中读取并返回其数据。谢谢!

python eclipse selenium automated-tests robotframework
1个回答
0
投票

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

首先,您需要能够将当前行数存储在某处。因此,一种显而易见的解决方案是将数字写入文件到磁盘。为此,您需要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库。检查文档here

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

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}

[注意,我正在使用Split Into Lines库中的String关键字和Get From List库中的Collections关键字。检查文档herehere。所以我需要更新导入:

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

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

仅以一个完整的工作示例进行总结:

*** 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的实现细节。我想最大的麻烦是找出如何在某处存储行数并在下次加载它,这在我的回答中已经显示。

© www.soinside.com 2019 - 2024. All rights reserved.