简体   繁体   English

Selenium Python 错误'对象没有属性驱动程序'

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

I have two files one is Login_test.py and the other is Financial_Account_Balance.py .我有两个文件,一个是Login_test.py ,另一个是Financial_Account_Balance.py When I run the Login file, it works but I want after the system Login, it should check the Financial Account.当我运行登录文件时,它可以工作,但我希望在系统登录后,它应该检查财务帐户。 I keep getting error when I instantiate the Class in the Financial Account Script.在财务帐户脚本中实例化 Class 时,我不断收到错误消息。

Object has no attribute 'driver'

Below is my code for both files下面是我的两个文件的代码

Login_test登录测试

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

Financial Account File财务账户文件

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

Error I get when I run the Financial Account file运行财务帐户文件时出现错误

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

if driver is none, you are not creating any self.driver.如果驱动程序为无,则您没有创建任何 self.driver。 so that class will not have any instance variable driver in that case这样 class 在这种情况下将没有任何实例变量驱动程序

fix:使固定:

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.

相关问题 Python-Selenium Web驱动程序错误-self._driver.execute-AttributeError:“ unicode”对象没有属性“ id” - Python - Selenium Web Driver error - self._driver.execute - AttributeError: 'unicode' object has no attribute 'id' pycharm- python 与 selenium web 驱动程序。 得到错误 object has no attribute driver - pycharm- python with selenium web driver. Get error object has no attribute driver Python Selenium'WebDriver'对象没有属性错误 - Python Selenium 'WebDriver' object has no attribute error Selenium 'FirefoxWebElement' object 没有属性 '_driver' - Selenium 'FirefoxWebElement' object has no attribute '_driver' selenium python 错误“dict”对象没有属性“click” - selenium python error 'dict' object has no attribute 'click' Python selenium &#39;list&#39; 对象没有属性 &#39;text&#39; 错误 - Python selenium 'list' object has no attribute 'text' error selenium python send_key 错误:列表 object 没有属性 - selenium python send_key error: list object has no attribute AttributeError:Selenium Webdriver中的python对象没有属性 - AttributeError: object has no attribute in python in selenium webdriver AttributeError:“ WebElement”对象没有属性(python)(硒) - AttributeError: 'WebElement' object has no attribute (python) (selenium) Python - 对象没有属性Error - Python - object has no attribute Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM