Skip to content Skip to sidebar Skip to footer

Python Webdriver Attributeerror: Loginpage Instance Has No Attribute 'driver'

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. To start off with I am trying to write a

Solution 1:

Firstly, there is a flaw in your design.

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.

Your setup should look something like this,

Page file:

# 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
.

Test File:

.
.
    classGoogleTest(unittest.TestCase):deftest_valid_login(self):
        self.driver = webdriver.Firefox()   # the first 2 stmts can be in a setupclassself.driver.get("http://www.testaaa.com")
        log_in = LoginPage.LoginPage(self.driver)
        log_in.userLogin_valid()
.
.

Solution 2:

I've had this issue a couple of times and every time I found it due to a mismatch between my Chrome browser version and the Chrome Webdriver version.

So, in your Chrome browser check Help>About Google Chrome before downloading a corresponding ChromeDriver from https://chromedriver.chromium.org/.

Good luck!

Post a Comment for "Python Webdriver Attributeerror: Loginpage Instance Has No Attribute 'driver'"