Search code examples
pythonseleniumselenium-webdriverxpathwebdriverwait

selenium - wait when editbox is interactable (after button click)


So I'm trying to learn about interacting with elements, after they are loaded (or enabled/interactable). In this case pressing button enables Edit box (after like 3-4secs), so you can write something. Here's link: http://the-internet.herokuapp.com/dynamic_controls

Here is how it looks now - works, but what if this edit-box would load, for example, 6 seconds? Then it'd be wrecked up...

enable = browser.find_element(By.XPATH, "/html/body/div[2]/div/div[1]/form[2]/button")
enable.click()
time.sleep(5)
fillform = browser.find_element(By.XPATH, "/html/body/div[2]/div/div[1]/form[2]/input")           
fillform.send_keys("testtt")
time.sleep(1)

I also tried browser.implicitly_wait(20) but it is like ignored and does nothing. Browser just keeps closing, because it can't find ENABLED edit box. It gives the error:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

I've tried another method for this - didn't work, as well...

element = WebDriverWait(browser, 5).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div/div[1]/form[2]/input")))

I am using Chrome+Python.


Solution

  • Use WebDriverWait() and wait for element_to_be_clickable(). Also use the following xpath option.

    driver.get('http://the-internet.herokuapp.com/dynamic_controls')
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Enable']"))).click()
    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//form[@id='input-example']//input"))).send_keys("testtt")
    

    browser snapshot:

    enter image description here