我正在尝试在 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/