AlienHead
AlienHead

Reputation: 125

Selenium Python Behave WebDriverWait

I am trying to use WebDriverWait for the page to wait until it goes from login page to User panel page.

from features.browser import Browser
from features.locators import LoginPageLocators
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class LoginForm(Browser):
    def log_in_as(self, username, password):
        username_field = self.driver.find_element(*LoginPageLocators.EMAIL_FIELD)
        password_field = self.driver.find_element(*LoginPageLocators.PASSWORD_FIELD)
        username_field.send_keys(username)
        password_field.send_keys(password)
        submit_btn = self.driver.find_element(*LoginPageLocators.LOGIN_BTN)
        submit_btn.click()

        try:
            element = WebDriverWait(self, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ssr-webnav"]/div/div[1]/nav[2]/div[2]')))
        finally:
            print("Log In Succesfull")

Then I call it in steps by

@step(u'I log in as registered user')
def step_impl(context):
    context.login_form.log_in_as("[email protected]", "password123")

In environment.py I link them by

def before_all(context):
    context.browser = Browser()
    context.login_form = LoginForm()

Logging in works as intended, but when I added the WebdriverWait it throws an error:

Traceback (most recent call last):
  File "\behave\model.py", line 1329, in run
    match.run(runner.context)
  File "\behave\matchers.py", line 98, in run
    self.func(context, *args, **kwargs)
  File "features\steps\steps.py", line 13, in step_impl
    context.login_form.log_in_as("[email protected]", "password123")
  File "\features\pages\login.py", line 27, in log_in_as
    EC.presence_of_element_located((By.XPATH, '//*[@id="ssr-webnav"]/div/div[1]/nav[2]/div[2]')))
  File "\webdriver\support\wait.py", line 71, in until
    value = method(self._driver)
  File "\webdriver\support\expected_conditions.py", line 63, in __call__
    return _find_element(driver, self.locator)
  File \webdriver\support\expected_conditions.py", line 397, in 
    _find_element
    return driver.find_element(*by)
  AttributeError: 'LoginForm' object has no attribute 'find_element'

Any Ideas what am I missing? Thanks

Upvotes: 1

Views: 1085

Answers (1)

natn2323
natn2323

Reputation: 2061

From looking at Selenium documentation for Python, it seems you need to pass in a webdriver object to WebDriverWait(...). And you're passing in self, rather than self.driver, into the function call.

So this

                        vvvv
element = WebDriverWait(self, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ssr-webnav"]/div/div[1]/nav[2]/div[2]')))

needs to be this

element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ssr-webnav"]/div/div[1]/nav[2]/div[2]')))

Upvotes: 4

Related Questions