简体   繁体   English

抛出错误:AttributeError:'KenLogin'对象没有属性'driver'

[英]Throwing error : AttributeError: 'KenLogin' object has no attribute 'driver'

I am trying to run simple code for login page by using unittest in python but facing this error. 我试图通过在python中使用unittest为登录页面运行简单的代码,但遇到此错误。 Can anyone gives answer for this ? 谁能给出答案?

I am trying to run simple code for login page by using unittest in python but facing this error. 我试图通过在python中使用unittest为登录页面运行简单的代码,但遇到此错误。 Can anyone gives answer for this ? 谁能给出答案?

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class KenLogin(unittest.TestCase):



    def setup(self):

       # self.driver = webdriver.Chrome(executable_path='E:\Learning\chromedriver.exe')
        self.driver = webdriver.Firefox()

    def test_login(self):

        driver = self.driver
        driver.get("http://qa.kenzen.com")
        username = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[1]/input")
        password = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[2]/input")

        username.send_keys("pardeepk@clovity.com")
        password.send_keys("Kenzen@123")

        button = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/button")

        button.click()
        print("Login Pass")

    def tearDown(self):
        self.driver.close()


if __name__ == "__main__":
      unittest.main()

--------------------------------------------------------------------------------

Finding files... done.
Importing test modules ... done.

======================================================================
ERROR: test_login (kenZen.KenLogin.KenLogin)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 18, in test_login
    driver = self.driver
AttributeError: 'KenLogin' object has no attribute 'driver'

======================================================================
ERROR: test_login (kenZen.KenLogin.KenLogin)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 32, in tearDown
    self.driver.close()
AttributeError: 'KenLogin' object has no attribute 'driver'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=2)

Create a global variable and pass that value in the function as shown below: 创建一个全局变量并在函数中传递该值,如下所示:

class KenLogin(unittest.TestCase):

    def globe(self):

        global driver

        driver = webdriver.Chrome(executable_path='E:\Learning\chromedriver.exe')

    def test_login(self):


        driver.maximize_window()
        driver.implicitly_wait(2000)
        driver.get("http://qa.kenzen.com")
        username = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[1]/input")
        password = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[2]/input")

        username.send_keys("pardeepk@clovity.com")
        password.send_keys("Kenzen@123")

        button = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/button")

        button.send_keys(Keys.ENTER)
        print("Login Pass")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pyinstaller 抛出 AttributeError: 'NoneType' object has no attribute 'groups' 错误 - Pyinstaller throwing AttributeError: 'NoneType' object has no attribute 'groups' error AttributeError:'LoginPage'对象没有属性'driver' - AttributeError: 'LoginPage' object has no attribute 'driver' AttributeError:'TestLogin'对象没有属性'driver' - AttributeError: 'TestLogin' object has no attribute 'driver' AttributeError: 'WebDriver' object 没有属性 'driver' - AttributeError: 'WebDriver' object has no attribute 'driver' AttributeError:“WebElement”object 没有属性“驱动程序” - AttributeError: 'WebElement' object has no attribute 'driver' AttributeError:“驱动程序”对象没有属性“ id” - AttributeError: 'Driver' object has no attribute 'id' AttributeError: 'TestTomsLogin' object 没有属性 'driver' - AttributeError: 'TestTomsLogin' object has no attribute 'driver' Python-Selenium Web驱动程序错误-self._driver.execute-AttributeError:“ unicode”对象没有属性“ id” - Python - Selenium Web Driver error - self._driver.execute - AttributeError: 'unicode' object has no attribute 'id' 为什么 python 抛出错误:AttributeError: 'numpy.ndarray' object has no attribute 'append'? - Why is python throwing error : AttributeError: 'numpy.ndarray' object has no attribute 'append'? Python 3.6 抛出日志错误,因为“AttributeError: 'tuple' object has no attribute 'find'' - Python 3.6 throwing logging error as 'AttributeError: 'tuple' object has no attribute 'find''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM