0

I am starting with Python - Selenium and I cannot interact with the password section and insert the password from the text file. The problem is not with inserting or locating the element I think because the username locating and insert works correct. The problem is probably in waits, but I cannot formulate this command in code correctly. After the username is inserted and the page is swiped to next page and there should be some dalay. So I tried function "time.sleep(4)" and it does not work but even the function "driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS)" not works too. I try many options of formulate this command but the code ends always by Message: element not interactable. Please could you help me with this problem? Here is problematic part of code:

driver.find_element_by_id("userName").send_keys(username2)
driver.find_element_by_id("verify_user_btn").click()

time.sleep(4)
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS)
driver.find_element_by_id("password").send_keys(password2)
driver.find_element_by_id("btnSubmit").click()

Many thanks.

Sinda
  • 5
  • 3

2 Answers2

1

Use explicit wait instead of an implicit wait. Ask selenium to wait till the element password is visible. Also before clicking on the button check if the button is displayed and is enabled.

//for password wait.Until(driver => driver.FindElement(By.ID("password")).Displayed;

//for button wait.Until(driver => driver.FindElement(element).Enabled && driver.FindElement(element).Displayed;

Note: These are the C# codes.

Zmehak
  • 11
  • 4
0

Here are some python methods to wait for either element to be displayed, clickable or present in the DOM. Just pass in the By type such as ID, Name, etc. and the locator. For exampe pass in By.ID and 'password'

def wait_for_element_presence(self, by_type, locator, wait_seconds=10):
    self.log.debug("Waiting for presence of element located")
    result_flag = False
    try:
        WebDriverWait(self.driver, wait_seconds).until(ec.presence_of_element_located((
            by_type, locator)))
        result_flag = True
    except Exception as e:
        self.log.warning("An exception occurred, see below for details: ")
        self.log.error(e, exc_info=True)
    return result_flag


def wait_for_element_to_be_displayed(self, by_type, locator, wait_seconds=10):
    self.log.debug("Waiting for element to be displayed")
    result_flag = False
    try:
        WebDriverWait(self.driver, wait_seconds).until(
            lambda x: x.find_element(by_type, locator).is_displayed())
        result_flag = True
    except TimeoutException:
        self.log.error("User supplied element locator: by {0} '{1}'".format(by_type, locator))
    return result_flag

def wait_for_element_to_be_clickable(self, by_type, locator, wait_seconds=10):
    self.log.debug("Waiting for element to be clickable")
    result_flag = False
    try:
        WebDriverWait(self.driver, wait_seconds).until(ec.element_to_be_clickable((
            by_type, locator)))
        result_flag = True
    except Exception as e:
        self.log.warning("An exception occurred, see below for details: ")
        self.log.error(e, exc_info=True)
    return result_flag
JerryBringer
  • 150
  • 1
  • 1
  • 8