无法使用python在机器人中创建自定义关键字

问题描述 投票:0回答:1
from robot.api.deco import library,keyword
from robot.libraries.BuiltIn import BuiltIn

@library
class Shop:
    def __init__(self):
        self.selLib = BuiltIn().get_library_instance("SeleniumLibrary")

最后一行不起作用,它在机器人文件中给出了一些错误 getting this error in the robot file

应该能够创建自定义关键字

python-3.x robotframework
1个回答
0
投票

该错误不是来自 Robot Framework。它来自 Robotcode LSP,这只是 IDE 的一个功能。实际运行代码时不会出现此错误。

所以,当robotcode扫描你的代码时 - 它会尝试初始化你的类,以找出它将公开哪些关键字 - 但因为它的lsp,而不是机器人框架,机器人没有运行,因此你正在调用需要机器人的功能框架内部,你会得到这个异常。

你可以:

  1. 捕获 RobotNotRunningError 并且不关心它,因为本质上,当 Robot 未运行时,您不需要引用 SeleniumLibrary,如下所示;
try:
  self.selLib = BuiltIn().get_library_instance("SeleniumLibrary")
except RobotNotRunningError:
  # safe to ignore in most cases
  pass
  1. 使用SeleniumLibrary的插件功能; https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst#Plugins
© www.soinside.com 2019 - 2024. All rights reserved.