2017-03-09 10 views
1

方法です:TypeErrorを扱う方法:__call __()は少なくとも2つの引数をとります(1つは指定されています)?一部の下

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)

答えて

2

KeyMouseOperationは、列挙型クラスであるため、呼び出す必要はありません。このようにそれを使用すると

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) 

:代わりに、keymouseクラスメソッドを作る

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

enumドキュメントのAllowed members and attributes of enumerations sectionを参照してください。

個人的に、私は価値にkeymouse方法を行い、ActionChainsクラスに使用する正しい方法を追跡したい:

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

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

はその後、としてこれを使用します

関連する問題