python - 如何处理TypeError : __call__() takes at least 2 arguments (1 given)?

标签 python selenium testing selenium-webdriver automated-tests

下面是方法:

from enum import Enum
from selenium import webdriver
import  time
from selenium.webdriver.common.action_chains import ActionChains

class KeyMouseOperation(Enum):
    CONTEXT_CLICK = 1
    DOUBLE_CLICK = 2
    DRAG_AND_DROP = 3
    CLICK_AND_HOLD = 4

    def keymouse(url, operation_by , elementxpath):
        driver = webdriver.Chrome()
        driver.get(url)
        time.sleep(1)
        driver.maximize_window()
        time.sleep(1)

        if operation_by == KeyMouseOperation.CONTEXT_CLICK:
            result = ActionChains(driver).context_click(driver.find_element_by_xpath(elementxpath)).perform()
        if operation_by == KeyMouseOperation.DOUBLE_CLICK:
            result = ActionChains(driver).double_click(driver.find_element_by_xpath(elementxpath)).perform()
        if operation_by == KeyMouseOperation.DRAG_AND_DROP:
            result = ActionChains(driver).drag_and_drop(driver.find_element_by_xpath(elementxpath), driver.find_element_by_xpath(elementxpath)).perform()
        if operation_by == KeyMouseOperation.CLICK_AND_HOLD:
            result = ActionChains(driver).click_and_hold(driver.find_element_by_xpath(elementxpath)).perform()
        else:
            time.sleep(3)

这是方法的实例:

from method.key_Mouse import *
KM = KeyMouseOperation()
KM.keymouse("https://www.baidu.com", KeyMouseOperation.CONTEXT_CLICK, "//*[@id='kw']")

但结果是:

C:\Python27\python.exe C:/Users/chenjia/PycharmProjects/AutomationTestSuite/Case/practice.py
Traceback (most recent call last):
  File "C:/Users/chenjia/PycharmProjects/AutomationTestSuite/Case/practice.py", line 46, in <module>
    KM = KeyMouseOperation()
TypeError: __call__() takes at least 2 arguments (1 given)

Process finished with exit code 1

如何处理 TypeError: __call__() takes at least 2 arguments (1 given)

最佳答案

KeyMouseOperation 是一个枚举类,你不必调用它。相反,将 keymouse 设为 classmethod:

class KeyMouseOperation(Enum):
    CONTEXT_CLICK = 1
    DOUBLE_CLICK = 2
    DRAG_AND_DROP = 3
    CLICK_AND_HOLD = 4

    @classmethod
    def keymouse(cls, url, operation_by, elementxpath):
        driver = webdriver.Chrome()
        driver.get(url)
        time.sleep(1)
        driver.maximize_window()
        time.sleep(1)

        if operation_by == cls.CONTEXT_CLICK:
            result = ActionChains(driver).context_click(
                driver.find_element_by_xpath(elementxpath)).perform()
        if operation_by == cls.DOUBLE_CLICK:
            result = ActionChains(driver).double_click(
                driver.find_element_by_xpath(elementxpath)).perform()
        if operation_by == cls.DRAG_AND_DROP:
            result = ActionChains(driver).drag_and_drop(
                driver.find_element_by_xpath(elementxpath), driver.find_element_by_xpath(elementxpath)).perform()
        if operation_by == cls.CLICK_AND_HOLD:
            result = ActionChains(driver).click_and_hold(
                driver.find_element_by_xpath(elementxpath)).perform()
        else:
            time.sleep(3)

然后像这样使用它:

KeyMouseOperation.keymouse(
    "https://www.baidu.com", KeyMouseOperation.CONTEXT_CLICK,
    "//*[@id='kw']")

参见 Allowed members and attributes of enumerations section enum 文档。

就我个人而言,我会让 keymouse 成为值的 method,并跟踪在 ActionChains 类上使用的正确方法:

class KeyMouseOperation(Enum):
    # each value is the method (unbound), and how many arguments
    # to pass in.
    CONTEXT_CLICK = (ActionChains.context_click, 1)
    DOUBLE_CLICK = (ActionChains.double_click, 1)
    DRAG_AND_DROP = (ActionChains.drag_and_drop, 2)
    CLICK_AND_HOLD = (ActionChains.click_and_hold, 1)

    def keymouse(self, url, elementxpath):
        driver = webdriver.Chrome()
        driver.get(url)
        time.sleep(1)
        driver.maximize_window()
        time.sleep(1)

        ac = ActionChains(driver)
        action, argcount = self.value
        method = action.__get__(ac)  # bind the action method
        element = driver.find_element_by_xpath(elementxpath)
        return method(*[element] * argcount).perform()

然后将其用作:

KeyMouseOperation.CONTEXT_CLICK("https://www.baidu.com", "//*[@id='kw']") 

关于python - 如何处理TypeError : __call__() takes at least 2 arguments (1 given)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42698109/

相关文章:

python - 如何点击图像selenium python

python - 创建驱动基类并继承webdriver

使用 Jasmine 和 Sinon 在匿名函数中测试 Backbone 代码

python - 导入错误: No module named 'support'

python - 如何删除列表中每个列表中的最后一个元素

selenium - Windows 10 - chromedriver.exe 返回我只允许本地连接

在 Haskell 中调试无限 Sum

reactjs - 使用 Recompose 测试 HOC

python - 从压缩数据列表创建一个非常大的稀疏矩阵 csv

python - 如何在 ipdb 中打印 p 变量