我在修改同一個 XML 中具有相同 Xpath 的多個元素時遇到問題。這是代碼:
*** Settings ***
Library XML
Library String
*** Variables ***
${XMLpath} AbsolutePath.xml
${Xpath} GM/BODY/CMss/message #(there are two elements with this xpath)
*** Test Cases ***
TestCase
${root} Parse Xml ${XMLpath}
@{CMmessage} Get Elements Text ${root} ${Xpath}
${CMmessage1} Set Variable @{CMmessage}[1]
#...CMmessage1 modifications...
Set ${root} ${CMmessage1} \ ${Xpath} #Here is failing due there are multiple elements (2) matching the XPath
${CMmessage2} Set Variable @{CMmessage}[2]
#...CMmessage2 modfications...
Set ${root} ${CMmessage1} \ ${Xpath} #Here is failing due there are multiple elements (2) matching the XPath
Save Xml ${root} ${XMLpath}
有問題,給出的Xpath不是唯一的。我試過索引:
Set ${root} ${CMmessage1} \ ${Xpath}[1]
Set ${root} ${CMmessage1} \ ${Xpath}[2]
但這沒用……有誰知道如何處理 Robotframework-XMLlibrary 中的索引?
uj5u.com熱心網友回復:
您需要對這些元素進行一些處理。
使用獲取元素關鍵字。
uj5u.com熱心網友回復:
好的,終于找到問題所在了。使用
Set ${root} ${CMmessage1} \ ${Xpath}[1]
這是錯的
@{CMmes1} Get Element Text ${root} ${Xpath}
@{CMmes2} Get Element Text ${root} ${Xpath}
...
Set ${root} ${CMmes1} \ GM/BODY/CMss[1]/message
Set ${root} ${CMmes2} \ GM/BODY/CMss[2]/message
沒關系。關鍵是我有 2 個 CMmes,而不是 CMes 中的 2 條訊息。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/369178.html
