【问题标题】:How to call methods from class located in one python file from another python file如何从位于另一个python文件的一个python文件中的类中调用方法
【发布时间】:2019-05-29 00:27:52
【问题描述】:

我正在使用 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]

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    只需如下更改__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")
    

    【讨论】:

    • 非常感谢您的回答!
    【解决方案2】:

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

    我不确定您使用的是 Python 2 还是 Python 3,但以下是如何在 2 中使您的方法成为静态:https://docs.python.org/2/library/functions.html#staticmethod

    【讨论】:

    • 非常感谢您的回答!
    【解决方案3】:

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

    【讨论】:

    • 我对编码和试图弄清楚仍然很陌生。所以如果我这样改变它:action = Actions(driver) 它会给我一个错误: Traceback (last recent call last): File "/Users/a./Desktop/Automation_Work/correct_PPLS/oop/Test.py", line 9、在 action = Actions(driver) NameError: name 'driver' is not defined [Finished in 0.162s]
    • 您的__init__ 方法有一个名为driver 的参数。您必须为其提供一个值。该值不必命名为driver,在这种情况下可能不应该命名。根据您的设置,我猜您希望它是 webdriver.Chrome()
    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2018-12-31
    • 2019-04-24
    • 1970-01-01
    • 2021-01-29
    • 2013-06-16
    • 2021-03-07
    • 2013-05-17
    相关资源
    最近更新 更多