Python WebDriver AttributeError:LoginPage实例没有属性"driver" [英] Python WebDriver AttributeError: LoginPage instance has no attribute 'driver'

查看:120
本文介绍了Python WebDriver AttributeError:LoginPage实例没有属性"driver"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关Python Selenium Webdriver Page Object模型的教程,因为我必须使用Selenium和Python来自动进行gui测试.

I have read a few tutorials on Python Selenium Webdriver Page Object model as I have to automate the gui tests using Selenium with Python.

首先,我试图编写一个Login Page类和一个LoginMainTest类.运行代码时出现以下错误.

To start off with I am trying to write a Login Page class and a LoginMainTest class. I am getting the following error when i run the code.

AttributeError:LoginPage实例没有属性驱动程序"

AttributeError: LoginPage instance has no attribute 'driver'

我认为我必须在实例化LoginPage的地方指定Selenium驱动程序 例如在这一行上log_in = LoginPage.LoginPage()

I think i have to specify the selenium driver where i instantiate the LoginPage e.g. on this line log_in = LoginPage.LoginPage()

请给我一些帮助.

完整错误:

Traceback (most recent call last):
File "E:\Python projects\unitTest_sample - Modifying into Page Object\LoginMainTest.py", line 11, in test_valid_login
log_in = LoginPage.LoginPage()
File "E:\unitTest_sample - Modifying into Page Object\LoginPage.py", line 20, in __init__
emailFieldElement  = self.driver.find_element_by_id(self.emailFieldID)
AttributeError: LoginPage instance has no attribute 'driver'

我的LoginMainTest.py类如下:

My LoginMainTest.py class is as follows:

import LoginPage
import unittest

class GoogleTest(unittest.TestCase):

  def test_valid_login(self):
    log_in = LoginPage.LoginPage()
    log_in.userLogin_valid()

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

我的Login.py类如下:

My Login.py class is as follows:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait 

class LoginPage():

Username            = "test1"
password            = "Test"
emailFieldID        = "email"
passFieldID         = "pass"
loginButtonXpath    = "//input[@value='log in']"
logo_xpath          = "//a[contains(@href, 'logo')])[1]"

def setup(self):
    self.driver = webdriver.Firefox()
    self.driver.get("http://www.testaaa.com")

def __init__(self):
    emailFieldElement  = self.driver.find_element_by_id(self.emailFieldID)
    passFieldElement   = self.driver. find_element_by_id(self.passFieldID)
    loginFieldElement  =  self.driver.find_element_by_xpath(self.loginButtonXpath)

def userLogin_valid(self):
    self.emailFieldElement.clear()
    self.emailFieldElement.send_keys(self.Username)
    self.passFieldElement.clear()
    self.send_keys(self.password)
    self.loginFieldElement.click()

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

推荐答案

首先,您的设计存在缺陷.

Firstly, there is a flaw in your design.

脚本失败的原因是,当您创建登录页面的对象时,会调用 init ,但是由于在安装程序fn中定义了该驱动程序(从未调用过),因此无法找到驱动程序

The reason your script is failing because when you create the object of login page the init gets called but it fails to find the driver since it is defined in the setup fn (which is never called)

理想情况下,在页面对象模型中,应在测试文件中初始化浏览器(驱动程序),然后在创建任何页面文件的对象时应传递该驱动程序.

Ideally in the page object model you should initialize your browser(driver) in your test file and then while creating a object of any page file you should pass that driver.

您的设置应如下所示,

页面文件:

# setup() fn not needed here
.
.
def __init__(self, driver):
    self.driver = driver
    emailFieldElement  = self.driver.find_element_by_id(self.emailFieldID)
    passFieldElement   = self.driver. find_element_by_id(self.passFieldID)
    loginFieldElement  =  self.driver.find_element_by_xpath(self.loginButtonXpath)
.
# teardown() not needed here, should be in test file
.

测试文件:

.
.
    class GoogleTest(unittest.TestCase):

      def test_valid_login(self):
        self.driver = webdriver.Firefox()   # the first 2 stmts can be in a setupclass
        self.driver.get("http://www.testaaa.com")
        log_in = LoginPage.LoginPage(self.driver)
        log_in.userLogin_valid()
.
.

这篇关于Python WebDriver AttributeError:LoginPage实例没有属性"driver"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆