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

标签 python python-2.7 selenium-webdriver

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

登录页面.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'''

程序.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 驱动程序并将引用传递给“驱动程序”。此“驱动程序”作为参数传递给 LoginPage()。 LoginPage.py 无法使用“驱动程序”。谁能帮我找出这里的错误?

回溯:

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

最佳答案

好的,我明白你的问题了:

这很简单,在 LoginPage__init__ 中,您正在调用 self.driver.title() 然而,根据基本的 selenium example在他们的页面上,它只是 self.driver.title(没有括号)。

title 是一个属性而不是可调用方法,因此是错误。

关于python - 如何将 python 对象从一个文件模块传递到另一个文件模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16686751/

相关文章:

java - 无法在 selenium Web 驱动程序中使用 Javascript 设置选择值

python - 如何允许用户将可变大小的数据矩阵输入到 Python 中

python - 在 Python 中绘制网络/关系/连接类型图

python - Union 赋值中的不兼容类型

python - 三维数据集

python - 如何向 Bokeh DataTable 添加回调?

python - 返回确切的 python 函数的最佳方法只是作为单个可重复字符串运行

maven - 如何使用 Selenium 2.0 和 Maven 运行 HTML Selenese 测试?

python - 重新排序簇号以实现正确对应

java - 无法使用 Java 在 Selenium Web-Driver 中执行拖放