簡體   English   中英

Selenium Python 錯誤'對象沒有屬性驅動程序'

[英]Selenium Python error 'object has no attribute driver'

我有兩個文件,一個是Login_test.py ,另一個是Financial_Account_Balance.py 當我運行登錄文件時,它可以工作,但我希望在系統登錄后,它應該檢查財務帳戶。 在財務帳戶腳本中實例化 Class 時,我不斷收到錯誤消息。

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()

如果驅動程序為無,則您沒有創建任何 self.driver。 這樣 class 在這種情況下將沒有任何實例變量驅動程序

使固定:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM