如何将python对象从一个文件模块传递到另一个文件模块?

时间:2013-05-22 08:15:15

标签: python python-2.7 selenium-webdriver

我正在尝试在python和selenium中实现页面对象模型。就此而言,我有两个模块:

LoginPage.py

class LoginPage:

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

        '''do something with the self.driver object. 
           Example: Using self.driver.title() I can make sure if I am on the right page but it shows an error'''


    def Login(self, username, password):
        '''Use the same driver object for calling username and password objects of a page'''

Program.py

from selenium import webdriver

from LoginPage import *

class Program:

    driver = webdriver.Firefox()
    driver.get("https://www.linkedin.com/uas/login")

    #While creating LoginPage object, I'm passing driver object which is actually a firefox instance.
    login = LoginPage(driver)
    homePage = login.Login("sample@gmail.com", "mypassword")

    if homePage is not None:
        print "In Home page"

从Program.py模块,我正在创建一个Firefox驱动程序并将引用传递给'driver'。此“驱动程序”作为参数传递给LoginPage()。 LoginPage.py无法使用'driver'。任何人都可以帮我找到错误吗?

回溯:

Traceback (most recent call last): 
File "/media/Pinku/POMProject/src/Program.py", line 4, in <module> class Program: 
File "/media/Pinku/POMProject/src/Program.py", line 11, in Program login = LoginPage(driver) 
File "/media/Pinku/POMProject/src/LoginPage.py", line 5, in init if self.driver.title() == "Sign In": 
TypeError: 'unicode' object is not callable

1 个答案:

答案 0 :(得分:3)

对,我看到了你的问题:

这很简单,在__init__ LoginPage self.driver.title()中,您呼叫self.driver.title但是,根据其网页上的基本selenium example,它只是title (没有括号)。

{{1}}是属性而不是可调用方法,因此是错误。

相关问题