2

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
  • 1
    please show the full error. The code you posted shows no use of anything named execute. Also, please fix your indentation. Commented Feb 16, 2017 at 11:35
  • 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... Commented Feb 16, 2017 at 11:48

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

Then if you want to create another custom keywords in your CustomSeleniumLibrary on top of original Selenium2Library using S2L methods, simply call self._parent().s2l_method([args]).
Thank you, but inheriting Se2Lib is not an option, because I want to divide this class into few more and include them into my keywords and using inheritance results in ambiguous calls to Se2Lib functions.
Ahh. That's true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.