如何添加自定义函数以在 Selenium ActionChain/ActionBuilder 中运行?

h3y*_*y4w 5 python selenium

我曾经能够通过附加到 ActionChain 中的 _actions 变量来运行自定义函数,如下所示。

action = ActionChain(driver)
action._action.append(lambda: func())
Run Code Online (Sandbox Code Playgroud)

这似乎是现在,如果司机是W3C他们,而不是由ActionBuilder对象运行。ActionBuilder 类似乎没有像 ActionChains 那样按顺序运行操作的单一队列。任何对硒更熟悉的人都指出我正确的方向?

小智 0

如果您想完美地一致地指定操作,我建议您创建一个与您自己的 ActionBuild 一起使用的界面。把所有的行动都扔在那里并持续执行

像这样的东西

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()
Run Code Online (Sandbox Code Playgroud)