首页 > 解决方案 > 接收错误:没有名称为 '${json_data}= | 的关键字 评价 | json.load(open(${json_file})) | json'找到

问题描述

机器人框架相对较新。在带有 Chrome 的 Windows 10 笔记本电脑上使用机器人 4.0.3。在资源文件的顶部加载 Selenium 和 JSON 库。

使用具有以下内容的 JSON 文件:

{
     "first_name": "test",
     "last_name": "user1",
     "email": "test.user1@homelendingpal.com",
     "company": "test company",
     "phone": "919-123-4567",
     "message": "test user 1 test message"
    }

然后尝试使用资源机器人文件中的以下代码片段从中加载和提取值:

Click Submit Button
       ${json_data}= | Evaluate | json.load(open(${json_file})) | json
       ${name_first}= ${json_data["first_name"]}
       Input Text   //input[@id=input_1_1_3]   $name_first
       ${name_last}= ${json_data["last_name"]}
       Input Text   //input[@id=input_1_1_6]   $name_last
       ${email}= ${json_data["email"]}
       Input Text   //input[@id=input_1_2]   $email
       ${company}= ${json_data["company"]}
       Input Text   //input[@id=input_1_6]
       ${phone}= ${json_data["phone"]}
       Input Text   //input[@id=input_1_3]
       Click Element   xpath=//select[@id=input_1_4]/option[@value=Bank]
       ${message}= ${json_data["message"]}
       Click Element   id=gform_submit_button_1

我已经尝试了多种格式来加载文件并从谷歌搜索机器人框架 json 返回的各种示例中访问其中的元素。机器人总是在评估/加载语句上失败,并在标题中显示错误消息。

标签: jsonrobotframework

解决方案


您似乎更喜欢管道分隔格式,但缺少前导管道。根据机器人框架用户指南,您总是需要前导管道字符才能工作。

或者,如果您不使用管道,则行上的每个元素之间至少需要 2 个空格。

例如

| Click Submit Button
|  | ${json_data}= | Evaluate | json.load(open(${json_file})) | json

Click Submit Button
    ${json_data}=  Evaluate  json.load(open(${json_file}))  json

会是正确的。


推荐阅读