python - Selenium Python 错误“对象没有属性驱动程序”
问题描述
我有两个文件,一个是Login_test.py
,另一个是Financial_Account_Balance.py
. 当我运行登录文件时,它可以工作,但我希望在系统登录后,它应该检查财务帐户。在财务帐户脚本中实例化类时,我不断收到错误消息。
Object has no attribute 'driver'
下面是我的两个文件的代码
登录测试
import unittest
from selenium import webdriver
import time
class LoginForm(unittest.TestCase):
def __init__(self, driver = None):
#super().__init__(driver)
if driver is not None:
self.driver = driver
else:
self.setUp()
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r"..\browser\chromedriver.exe")
print("Running Set Up method")
print(self.driver)
self.test_result = None
def test_Login(self):
# We wrap this all in a try/except so we can set pass/fail at the end
try:
# load the page url
print('Loading Url')
self.driver.get('http://localhost:4200/')
# maximize the window
#print('Maximizing window')
self.driver.maximize_window()
# we'll start the login process by entering our username
print('Entering username:')
self.driver.find_element_by_name('username').send_keys('mobile@******.com')
# then by entering our password
print('Entering password:')
self.driver.find_element_by_id('pass').send_keys('*****')
# now we'll click the login button
print('Logging in')
self.driver.find_element_by_class_name("submit").click()
time.sleep(25)
self.test_result = 'pass'
except AssertionError as e:
# log the error message
self.test_result = 'fail'
raise
"""
def tearDown(self):
print("Done with session")
self.driver.quit()
"""
if __name__ == '__main__':
unittest.main()
财务账户文件
from Unit_Test_Files.Login_test import *
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time
import logging
import unittest
class FinancialAccountBalance:
#logf = open("Financial_Account_Balance_exception.log", "w")
def __init__(self, driver = None):
if driver is not None:
self.driver = driver
else:
print('Financial Account Balance Testing Started...')
self.test_finacial()
def setUp(self):
print(self.driver)
print("setup method running")
self.test_finacial()
def test_finacial(self):
try:
print(self.driver)
self.driver.find_element_by_xpath(
'/html/body/chankya-app/chankya-layout/div/ng-sidebar-container/div/div/section/div/div[2]/app-home/div/div/div/div/div[2]/div/input').send_keys(
'Financial Account Balance')
time.sleep(2)
self.driver.find_element_by_xpath(
'/html/body/chankya-app/chankya-layout/div/ng-sidebar-container/div/div/section/div/div[2]/app-home/div/div/div/div/div[2]/div/input').send_keys(
Keys.ENTER)
time.sleep(2)
WebDriverWait(self.driver, 10).until(
EC.presence_of_all_elements_located(
(By.XPATH, '//html/body/chankya-app/chankya-layout/div/ng-sidebar-container'
'/div/div/section/div/div[2]/app-home/div/div/div/div/div[4]'
'/div/div/table/tbody/tr')))
result = self.driver.find_element_by_xpath(
'(/html/body/chankya-app/chankya-layout/div/ng-sidebar-container/div/div/section/div/div[2]/app-home/div/div/div/div/div[4]/div/div/table/tbody/tr)[1]')
result.click()
time.sleep(15)
logging.basicConfig(filename='Financial_Account_Balance.log', level=logging.INFO, filemode='w',
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p ')
logging.info('Date and Time \nReport: Financial Account Balance Automation Test is Successful!!! \n')
print('Financial Account Balance Automation Test is Successful!!!')
except AssertionError as e:
# log the error message
self.test_result = 'fail'
raise
def test_verify(self):
lf = LoginForm()
lf.test_Login()
self.test_finacial()
if __name__ == '__main__':
fa = FinancialAccountBalance()
fa.test_verify()
运行财务帐户文件时出现错误
"C:\Users\Apollo Universe IDS\.virtualenvs\test-cM_nBYrn\Scripts\python.exe" C:/Users/Public/Documents/test/Financial_Module/Financial_Account_Balance.py
Financial Account Balance Testing Started...
Traceback (most recent call last):
File "C:/Users/Public/Documents/test/Financial_Module/Financial_Account_Balance.py", line 68, in <module>
fa = FinancialAccountBalance()
File "C:/Users/Public/Documents/test/Financial_Module/Financial_Account_Balance.py", line 21, in __init__
self.test_finacial()
File "C:/Users/Public/Documents/test/Financial_Module/Financial_Account_Balance.py", line 30, in test_finacial
print(self.driver)
AttributeError: 'FinancialAccountBalance' object has no attribute 'driver'
解决方案
def __init__(self, driver = None):
if driver is not None:
self.driver = driver
else:
print('Financial Account Balance Testing Started...')
self.test_finacial()
如果 driver 是 none ,则您没有创建任何 self.driver 。因此在这种情况下,该类将没有任何实例变量驱动程序
使固定:
class FinancialAccountBalance:
#logf = open("Financial_Account_Balance_exception.log", "w")
def __init__(self, driver = None):
if driver is not None:
self.driver = driver
else:
print('Financial Account Balance Testing Started...')
self.setUp()
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r"..\browser\chromedriver.exe")
print(self.driver)
print("setup method running")
self.test_finacial()
推荐阅读
- vue.js - Vue路由器不工作 - 默认为根路径
- odata - Azure 数据工厂 ODATA 到雪花数据库
- node.js - 删除数据表格数据库404错误API | 数据库 | 快递js
- ios - SwiftUI 在使用 Swift 结构和 id 时让 ScrollViewReader 滚动
- python - 如何调用/实例化链表
- azure - 在不重新启动 VM 的情况下调整 Azure VM 大小
- spring-boot - 在 application.properties 中使用 AD 服务主体设置 Spring Data JPA
- actions-on-google - 关联 Google Actions 的帐户是否适用于 Google TV 或 Android TV?
- gradle - GradleWrapperMain 和 GradleDaemon 进程使用了多少内存
- jmeter - 提供节点名或服务名,或在 Jmeter 中未知