UI.Vision宏添加条件分支后出现Unexpected Token错误求助
UI.Vision宏整合问题:循环删除后执行后续操作报错解决
我有一个可正常运行的UI.Vision宏,功能是循环删除第2行直至该行不存在。尝试添加「当第2行不存在时执行Part2操作」的逻辑后,出现Unexpected Token错误。以下是可正常运行的Part1代码及待整合的Part2代码:
Part 1(可正常运行:循环删除第2行)
[ { "Command": "label", "Target": "loopStart", "Value": "", "Description": "" }, { "Command": "store", "Target": "document.evaluate(\"//*[@id='cp2_lineItemGrid_gridView_lnkDelete_1']/div\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue != null", "Value": "elementExists", "Description": "" }, { "Command": "gotoIf", "Target": "${elementExists} == true", "Value": "", "Description": "" }, { "Command": "click", "Target": "xpath=//*[@id=\"cp2_lineItemGrid_gridView_lnkDelete_1\"]/div", "Value": "", "Targets": [ "xpath=//*[@id=\"cp2_lineItemGrid_gridView_lnkDelete_1\"]/div", "xpath=//tr[3]/td[18]/a/div", "css=#cp2_lineItemGrid_gridView_lnkDelete_1 > div" ], "Description": "" }, { "Command": "click", "Target": "xpath=//*[@id=\"cp2_ucRemoveConfirmation_btnYes\"]/span", "Value": "", "Targets": [ "xpath=//*[@id=\"cp2_ucRemoveConfirmation_btnYes\"]/span", "xpath=//div[21]/div/div[2]/div/button/span", "css=#cp2_ucRemoveConfirmation_btnYes > span" ], "Description": "" }, { "Command": "pause", "Target": "2500", "Value": "", "Description": "" }, { "Command": "gotoLabel", "Target": "loopStart", "Value": "", "Description": "" } ]
Part 2(待整合:第2行不存在时执行)
[ { "Command": "click", "Target": "xpath=//*[@id=\"btnNext\"]/span", "Value": "", "Targets": [ "xpath=//*[@id=\"btnNext\"]/span", "xpath=//button[5]/span", "css=#btnNext > span" ], "Description": "" }, { "Command": "click", "Target": "xpath=//*[@id=\"cp2_ucConfirmControl_btnContinue\"]/span", "Value": "", "Targets": [ "xpath=//*[@id=\"cp2_ucConfirmControl_btnContinue\"]/span", "xpath=//div[16]/div/div[2]/div[2]/button/span", "css=#cp2_ucConfirmControl_btnContinue > span" ], "Description": "" } ]
修正后的完整宏代码(解决Unexpected Token错误)
报错原因是整合时JSON格式不合法(如括号、逗号不匹配),同时需要调整gotoIf的逻辑,让元素不存在时跳转到Part2执行:
[ { "Command": "label", "Target": "loopStart", "Value": "", "Description": "" }, { "Command": "store", "Target": "document.evaluate(\"//*[@id='cp2_lineItemGrid_gridView_lnkDelete_1']/div\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue != null", "Value": "elementExists", "Description": "" }, // 元素不存在时,跳转到执行Part2的标签 { "Command": "gotoIf", "Target": "${elementExists} == false", "Value": "executePart2", "Description": "" }, // 元素存在时,执行删除操作 { "Command": "click", "Target": "xpath=//*[@id=\"cp2_lineItemGrid_gridView_lnkDelete_1\"]/div", "Value": "", "Targets": [ "xpath=//*[@id=\"cp2_lineItemGrid_gridView_lnkDelete_1\"]/div", "xpath=//tr[3]/td[18]/a/div", "css=#cp2_lineItemGrid_gridView_lnkDelete_1 > div" ], "Description": "" }, { "Command": "click", "Target": "xpath=//*[@id=\"cp2_ucRemoveConfirmation_btnYes\"]/span", "Value": "", "Targets": [ "xpath=//*[@id=\"cp2_ucRemoveConfirmation_btnYes\"]/span", "xpath=//div[21]/div/div[2]/div/button/span", "css=#cp2_ucRemoveConfirmation_btnYes > span" ], "Description": "" }, { "Command": "pause", "Target": "2500", "Value": "", "Description": "" }, { "Command": "gotoLabel", "Target": "loopStart", "Value": "", "Description": "" }, // Part2操作的标签 { "Command": "label", "Target": "executePart2", "Value": "", "Description": "" }, { "Command": "click", "Target": "xpath=//*[@id=\"btnNext\"]/span", "Value": "", "Targets": [ "xpath=//*[@id=\"btnNext\"]/span", "xpath=//button[5]/span", "css=#btnNext > span" ], "Description": "" }, { "Command": "click", "Target": "xpath=//*[@id=\"cp2_ucConfirmControl_btnContinue\"]/span", "Value": "", "Targets": [ "xpath=//*[@id=\"cp2_ucConfirmControl_btnContinue\"]/span", "xpath=//div[16]/div/div[2]/div[2]/button/span", "css=#cp2_ucConfirmControl_btnContinue > span" ], "Description": "" } ]
逻辑说明
- 每次进入
loopStart标签,先检查第2行的删除按钮是否存在 - 如果不存在,直接跳转到
executePart2标签执行后续操作 - 如果存在,执行删除、确认、暂停操作后,回到
loopStart继续检查 - Part2操作执行完毕后,宏自动结束,不会再循环
内容的提问来源于stack exchange,提问作者Ryan Rider
相关产品推荐
相关产品推荐

