2
votes

in my Robot framework tests I need some custom python keywords (e.g. to hold CTRL key) And everything worked before I started refactoring my "big" custom class (but I did not really change anything in this part around hold CTRL). Now I am getting AttributeError: 'Selenium2Library' object has no attribute 'execute'
My code is:

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()

and I just call "hold ctrl" directly in robot keyword then, the keyword file has my custom class imported as Library (and other custom keywords work)... Any idea why it fails on the "execute" please?

2
please show the full error. The code you posted shows no use of anything named execute. Also, please fix your indentation. - Bryan Oakley
well, there's nothing more in console...only "Check data in table XY for all values at the ... | FAIL | AttributeError: 'Selenium2Library' object has no attribute 'execute'" and yes, "execute" is nowhere in my code and it is not even inside of "perform" or "key_down" etc. I have really no idea what it means... - neliCZka

2 Answers

3
votes

The problem was in ActionChains, because it needs webdriver instance, not Se2Lib instance. Webdriver instance can be obtained by calling _current_browser(). I reworked it this way and it works:

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
votes

What about something like this:

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()