[英]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.