2

我需要一些定制的Python的关键字(如持有CTRL键) 和一切工作开始之前,我重构我的“大”自定义类(但我并没有真正改变任何东西在这部分左右按住CTRL键)。 现在我越来越AttributeError: 'Selenium2Library' object has no attribute 'execute'
我的代码是:的Python的机器人框架 - Se2Lib有没有属性“执行”在我的机器人框架的测试

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键”,那么,关键字文件有我的导入为库自定义类(和其他自定义关键字工作) ... 任何想法,为什么它在“执行”请失败?

+1

请显示完整的错误。您发布的代码显示不使用任何名为'execute'的东西。另外,请修复您的缩进。 –

+0

好吧,没有什么更多的控制台...“检查XY表中所有值的数据... | FAIL | AttributeError:'Selenium2Library'对象没有属性'执行'”,是的,“执行”是在我的代码中没有任何地方,它甚至不在“执行”或“key_down”之内等等。我真的不知道它是什么意思...... – neliCZka

回答

3

的问题是在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() 
1

什么是这样的:

class CustomSeleniumLibrary(Selenium2Library): 
    def __init__(self): 
     super(CustomSeleniumLibrary, self).__init__() 

    def _parent(self): 
     return super(CustomSeleniumLibrary, self) 

    def hold_ctrl(self): 
     ActionChains(self._current_browser()).send_keys(Keys.LEFT_CONTROL).perform() 
+0

然后,如果您想在您的CustomSeleniumLibrary中使用原始Selenium2Library的顶部创建另一个自定义关键字S2L方法,只需调用'self._parent()。s2l_method([args])''。 –

+0

谢谢,但继承Se2Lib不是一种选择,因为我想将这个类分成几个类,并将它们包含到我的关键字中,并使用继承结果对Se2Lib函数进行模糊调用。 – neliCZka

+0

啊。确实如此。 –