【问题标题】:Handling multiple XML elements with the same XPath处理具有相同 XPath 的多个 XML 元素
【发布时间】:2022-01-03 18:36:31
【问题描述】:

我在修改同一个 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 中的索引吗?

【问题讨论】:

    标签: xml xpath robotframework


    【解决方案1】:

    您需要对这些元素进行一些处理。

    使用Get Elements 关键字。

    【讨论】:

    • 您好 Helio,Get Elements 关键字会给我一个列表,其中包含所有具有相同路径的元素 OK。但是如果我想设置文本或设置任何属性(例如,将标签“message”替换为“message1”和“message2”)这是不可能的,因为设置属性使用路径,而不是元素......
    • 原则上,您可以像使用任何其他类型的定位器一样使用Get Elements 返回的对象。我“期待”与 SeleniumLibrary 中相同的行为,这是可能的。但是必须有一种方法来区分这些元素。否则,任何 API 或程序如何在它们上工作?
    • IDK 在 SeleniumLibrary 中的行为。这是像这样的“${elements}[0].Text”(使用获取元素而不是获取元素文本)
    • 对不起,我只能做实验。
    【解决方案2】:

    好的,我终于找到了问题所在。使用

    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 个消息。

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      相关资源
      最近更新 更多