How to add custom functions to run in Selenium ActionChain/ActionBuilder?

Viewed 209

I used to be able run custom function by appending to the _actions variable in ActionChain like below.

action = ActionChain(driver)
action._action.append(lambda: func())

It seems like now if drivers are w3c they are instead run by ActionBuilder object. The ActionBuilder class doesn't seem to have a singular queue to run actions in order like ActionChains. Anyone that is more familiar in selenium point me in the right direction?

1 Answers

If you want to beautifully consistently specify the actions, I advise you to make an interface to work with ActionBuild your own. Throw there all the actions and consistently perform

Something like this

class CustomActionChain:
    def __init__(self, driver):
        self.action = ActionChains(driver)
        self._actions = []

    def click(self, element):
        self._actions.append(lambda: self.action.click(element).perform())
        return self

    def custom_func(self, func):
        self._actions.append(func)
        return self

    def perform(self):
        for action in self._actions:
            action()
Related