如何从另一个python文件中的一个python文件中的类调用方法

时间:2019-05-29 00:27:52

标签: python selenium selenium-webdriver

我正在使用Selenium和Python创建框架。     我的数据驱动框架应包含(到目前为止)3个文件:

1) Config.py have class Config() that have all the nessesary methods such as: 
   def setUp() - 
   def tearDown()
   def click()
   def send_keys()

2) data.py - with all data 

3) test.py - with all of the steps

我目前正在研究click()方法。

我想将2个参数传递给此方法,它将使用slice()     确定我使用的是哪种定位器,然后单击     相应地。不幸的是,不管我做什么-它一直     丢给我一些错误。

config.py:

from   selenium import webdriver
from   selenium.webdriver.common.by import By
from   selenium.webdriver.common.keys import Keys
from   selenium.webdriver.support.ui import Select
from   selenium.common.exceptions import NoSuchElementException
from   selenium.common.exceptions import NoAlertPresentException

class Actions(object):

    def __init__(self, driver):
        self.driver = driver


    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)

    def tearDown(self):
        self.driver.quit()


    def click(self, elemLocator, elemValue):
        elp = elemLocator
        flp = slice(0,2)
        if elp[flp] == 'id':
            try:
                self.driver.find_element_by_id(elemValue).click()
            except:
                pass
        elif elp[flp] == 'xp':
            try:
                self.driver.find_element_by_xpath(elemValue).click()
            except:
                pass
        elif elp[flp] == 'li':
            try:
                self.driver.find_element_by_link_text(elemValue).click()
            except:
                pass
        elif elp[flp] == 'na':
            try:
                self.driver.find_element_by_name(elemValue).click()
            except:
                pass
        elif elp[flp] == 'cs':
            try:
                self.driver.find_element_by_css_selector(elemValue).click()
            except:
                pass
        elif elp[flp] == 'pa':
            try:
                self.driver.find_element_by_partial_link_text(elemValue).click()
            except:
                pass
        elif elp[flp] == 'ta':
            try:
                self.driver.find_element_by_tag_name(elemValue).click()
            except:
                pass
        elif elp[flp] == 'cl':
            try:
                self.driver.find_element_by_class_name(elemValue).click()
            except:
                pass


    def send_keys(self):
        pass

test.py:

from   selenium import webdriver
from   selenium.webdriver.common.by import By
from   selenium.webdriver.common.keys import Keys
from   selenium.webdriver.support.ui import Select
from   selenium.common.exceptions import NoSuchElementException
from   selenium.common.exceptions import NoAlertPresentException
from Setup import Actions

action = Actions()
action.setUp()

错误消息:

Traceback (most recent call last):
  File "/Users/a./Desktop/Automation_Work/correct_PPLS/oop/Test.py", line 9, in <module>
    action = Actions()
TypeError: __init__() takes exactly 2 arguments (1 given)
[Finished in 0.153s]

3 个答案:

答案 0 :(得分:0)

如果要调用非静态方法,则必须创建该类的实例。

我不确定您使用的是Python 2还是3,但是这是使方法在2中保持静态的方法:https://docs.python.org/2/library/functions.html#staticmethod

答案 1 :(得分:0)

您似乎在不带任何参数的情况下调用Action()。您实际上是在调用Action.__init__(self),而没有提供driver参数。这就是为什么您得到TypeError: __init__() takes exactly 2 arguments (1 given)的原因。给定的1是self,由Python自动提供,而缺少的1是driver

答案 2 :(得分:0)

只需如下更改__init__

def __init__(self,driver=None):
    if driver != None:
        self.driver = driver

现在,您可以通过或不通过驱动程序来调用__init__。但是,我建议将驱动程序声明在类之外,并使其成为全局类,以便您可以在任何地方访问它。

类似这样的东西:

from   selenium import webdriver
from   selenium.webdriver.common.by import By
from   selenium.webdriver.common.keys import Keys
from   selenium.webdriver.support.ui import Select
from   selenium.common.exceptions import NoSuchElementException
from   selenium.common.exceptions import NoAlertPresentException
driver = None
class Actions(object):

    def __init__(self, driver):
        self.driver = driver

    def setUp(self,browserName):
        global driver 
        if browserName == 'chrome' #<==== creating the driver based on browser (you can pass this as argument while calling this method)
            self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        # return driver #<==== return driver so that you can store with other name if you are planning to launch 2nd instance of driver.

action = Actions()
action.setUp("chrome")