Robot框架中的Python - Se2Lib没有属性 'execute'

标签 python selenium selenium-webdriver automated-tests robotframework

在我的机器人框架测试中,我需要一些自定义 python 关键字(例如按住 CTRL 键) 在我开始重构我的“大”自定义类之前,一切都正常(但我并没有真正改变这部分中围绕按住 CTRL 的任何内容)。 现在我收到 AttributeError: 'Selenium2Library' object has no attribute 'execute'
我的代码是:

class CustomSeleniumLibrary(object):
    def __init__(self):
        self.driver = None
        self.library = None

    def get_webdriver_instance(self):
        if self.library is None:
            self.library = BuiltIn().get_library_instance('Selenium2Library')
        return self.library

    def get_action_chain(self):
        if self.driver is None:
            self.driver = self.get_webdriver_instance()
            self.ac = ActionChains(self.driver)
        return self.ac

    def hold_ctrl(self):
        self.get_action_chain().key_down(Keys.LEFT_CONTROL)
        self.get_action_chain().perform()

我只需在机器人关键字中直接调用“按住ctrl”,然后关键字文件将我的自定义类导入为库(以及其他自定义关键字工作)... 知道为什么它在“执行”时失败吗?

最佳答案

问题出在 ActionChains 中,因为它需要 webdriver 实例,而不是 Se2Lib 实例。 Webdriver实例可以通过调用_current_browser()获得。我按照这种方式重新设计了它并且有效:

def get_library_instance(self):
    if self.library is None:
        self.library = BuiltIn().get_library_instance('Selenium2Library')
    return self.library

def get_action_chain(self):
    if self.ac is None:
        self.ac = ActionChains(self.get_library_instance()._current_browser())
    return self.ac

def hold_ctrl(self):
    actionChain = self.get_action_chain()
    actionChain.key_down(Keys.LEFT_CONTROL)
    actionChain.perform()

关于Robot框架中的Python - Se2Lib没有属性 'execute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42270896/

相关文章:

Python请求: Post Images on Facebook using Multipart/form-data

google-chrome - Selenium Chrome headless (headless) : Unable to receive message from renderer

ruby - 协议(protocol).rb :153:in `read_nonblock' : end of file reached (EOFError)

regex - 使用正则表达式的 selenium xpath 表达式中的语法错误

java - 想要检索给定 WebElement 的 Xpath

selenium - 使用 javascript 为 Web 元素设置文本 - Selenium Web 驱动程序

java - 如何使用 Selenium Java 循环遍历 div 中的每个元素?

python - 如何获取使用 Youtube-dl 下载的文件的文件名

python - 显式 cursor.close() 的必要性

python - 如何在 Flask、nginx、uwsgi 堆栈中重新加载 python 库?