本帖最后由 kettle_hou 于 2016-7-21 16:27 编辑
如题!我想把打开浏览器和用户登录作为两个class,放在同一个模块下使用。在调用用户登录类时,会报错!!
猜想:在def denglu(self):下的应该要调用class browser():创建的driver对象,不知道如何实现。请各位高手指点,谢谢。
代码如下:
#####new.py#####
from public import browser
from public import login
from selenium import webdriver
browser().firefox("此处是url")
yonghu = login("此处是username","此处是password")
yonghu.denglu()
#####public.py#####
from selenium import webdriver
import os
class browser():
# webdriver for chrome
def chrome(self,url,driverpath):
os.environ["webdriver.chrome.driver"] = driverpath
driver = webdriver.Chrome()
driver.get(url)
# webdriver for firefox
def firefox(self,url):
driver = webdriver.Firefox()
driver.get(url)
#login
class login():
def __init__(self,username,password):
self.name = username
self.password = password
def denglu(self):
driver = webdriver()
driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_4__userName']").send_keys(self.name)
driver.find_element_by_id("TANGRAM__PSP_4__password").send_keys(self.password)
driver.find_element_by_xpath("//*[@id='TANGRAM__PSP_4__submit']").click()
text = driver.find_element_by_id("TANGRAM__PSP_4__error").text
报错内容!!
Traceback (most recent call last):
File "F:\Workplace\Python auto testing\src\new1\__init__.py", line 6, in <module>
yonghu = login("此处是username","此处是password")
File "F:\Workplace\Python auto testing\src\new1\public.py", line 19, in __init__
a = self.driver
AttributeError: login instance has no attribute 'driver'
|