-1

I am writing an automated test with Selenium in Python. I do not understand why in the piece of code below the until method of WebdriverWait needs to be called twice, because otherwise the text input will not be filled. What do I need to add or delete so that the html input field is filled without the need to call to WebDriverWait.until() twice? The method looks like this:

class SomeClass:
    usernameId = 'username'
    passwordId = 'password'
    def doSomething(self, username, password):
        wait = WebDriverWait(self.driver, 10)
        sleep(2)
        # TODO: find out why one wait is not enough, and how it should be done properly.
        element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))
        # Why is the seconde line needed? Without it the input is not filled. It should not be?
        element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))
        element.send_keys(username)
        d = self.driver
        wait.until(lambda d: element.get_attribute("value") == username)

        pwdElement = wait.until(EC.element_to_be_clickable((By.ID, self.passwordId)))
        # Why is the seconde line needed? Without it the input is not filled. It should not be?
        pwdElement = wait.until(EC.element_to_be_clickable((By.ID, self.passwordId)))
        pwdElement.send_keys(password)
        wait.until(lambda d: pwdElement.get_attribute("value") == password)

        sleep(2)
        inloggenButton = wait.until(EC.element_to_be_clickable((By.ID, self.inloggenButtonId)))
        inloggenButton.click()
        GeneralPage(self.driver).logged_on()
TylerH
  • 20,799
  • 66
  • 75
  • 101
Daniel
  • 1
  • 2

1 Answers1

0

It looks absolutely redundant.
You can remove one of element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId))) lines and the sleep(2) too.
Possibly your site loads too much time and the 10 seconds timeout is not enough.
In this case you can set the timeout to 30 or even 60 seconds, as following:

class SomeClass:
    usernameId = 'username'
    passwordId = 'password'
    def doSomething(self, username, password):
        wait = WebDriverWait(self.driver, 60)

        # TODO: find out why one wait is not enough, and how it should be done properly.
        element = wait.until(EC.element_to_be_clickable((By.ID, self.usernameId)))

        element.send_keys(username)
        d = self.driver
        wait.until(lambda d: element.get_attribute("value") == username)


        pwdElement = wait.until(EC.element_to_be_clickable((By.ID, self.passwordId)))
        # Why is the seconde line needed? Without it the input is not filled. It should not be?
        pwdElement = wait.until(EC.element_to_be_clickable((By.ID, self.passwordId)))
        pwdElement.send_keys(password)
        wait.until(lambda d: pwdElement.get_attribute("value") == password)
    
        inloggenButton = wait.until(EC.element_to_be_clickable((By.ID, self.inloggenButtonId)))
        inloggenButton.click()
    GeneralPage(self.driver).logged_on()

Not sure about identation, copy-pasted it from your question...

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • I ran it in head mode, so I could see the web browser. The browser was just doing nothing, and only calling the wait until.element_to_be_clickable twice worked. I happened to find this page: https://stackoverflow.com/questions/31792301/locating-an-element-using-ng-model-using-selenium-in-python which mentions the method: elm = wait.until(EC.presence_of_element_located(( So I replaced the element_to_be_clickable with wait.until(EC.presence_of_element_located((By.ID, self.passwordId))) and now it seems to work. I will wait until tomorrow. Often things don't work anymore the next morning. :) – Daniel Jul 15 '21 at 17:35
  • Well, definitely not all the elements are clickable or visible. But if it is an element you want to click it must be clickable. Wait! Does that element inside the visible screen, not covered by some other element? Maybe you have to scroll to it to bring it into the view?.. can you share a web page you are working on? – Prophet Jul 15 '21 at 18:06