You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Robot Framework中如何修改非数字变量的值?(执行报错求助)

问题解决:Robot Framework条件修改变量报错

你遇到的报错是因为Run Keyword If的语法使用错误,该关键字不能直接在条件后写变量赋值语句,必须调用合法的Robot Framework关键字来完成变量修改。以下是两种正确的写法:

写法一:使用Run Keyword And Assign If(通用替换场景)

适合需要执行Replace String这类关键字来处理值的情况,直接将处理结果赋值给变量:

${deliveryfee} =  Get Text     xpath://*[@class='open-page auto-expand-transition']//*[@class='summary-row'][2]//*[@class='price total']
${deliveryfee} =  Run Keyword And Assign If    ${deliveryfee} == "-"    Replace String    ${deliveryfee}    -    0.00    ELSE    ${deliveryfee}

写法二:使用Run Keyword If + Set Variable(固定值替换场景)

如果只是把-替换成固定的0.00,用Set Variable更简洁:

${deliveryfee} =  Get Text     xpath://*[@class='open-page auto-expand-transition']//*[@class='summary-row'][2]//*[@class='price total']
Run Keyword If    ${deliveryfee} == "-"    Set Variable    ${deliveryfee}    0.00

错误原因说明

原代码中Run Keyword If后面直接写${deliveryfee} = Replace String不符合语法规则:Run Keyword If的参数要求是关键字名称和该关键字的参数,不能直接写赋值表达式。必须通过合法的关键字来实现变量值的修改或重新赋值。

内容的提问来源于stack exchange,提问作者Jolo Torrente

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 16:13:17