[Python] Selenium not triggering 'Save' button after modifying a placeholder field in...

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am automating a web page using Selenium + Python, and I need to update the Zip Code field. The expected behavior in the UI is:

    The "Save" button is hidden initially. When I click on the Zip Code field, the "Save" button appears but remains disabled. When I type something, the "Save" button becomes clickable. However, when I try to input text using Selenium, the "Save" button never appears or becomes clickable.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time

    # Start WebDriver
    driver = webdriver.Safari()
    driver.get("https://example.com/login")

    # Login
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "login-email"))).send_keys("admin@example.com")
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "login-password"))).send_keys("password" + Keys.RETURN)

    WebDriverWait(driver, 30).until(lambda x: "dashboard" in x.current_url)
    print("✅ Logged in!")

    # Navigate to the page
    driver.get("https://example.com/settings")

    # Modify "Zip Code" field
    zip_code_field = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "//input[@ng-model='company.zip']"))
    )

    # **Click the field to make the "Save" button appear**
    driver.execute_script("arguments[0].click();", zip_code_field)
    time.sleep(1) # Wait for the UI to update

    # **Ensure field is editable**
    driver.execute_script("arguments[0].removeAttribute('disabled');", zip_code_field)

    # **Enter a new Zip Code**
    zip_code_field.send_keys(Keys.CONTROL, "a") # Select all text
    zip_code_field.send_keys(Keys.BACKSPACE) # Delete existing text
    zip_code_field.send_keys("12345")
    time.sleep(1)

    # **Trigger AngularJS Change Detection**
    driver.execute_script("arguments[0].dispatchEvent(new Event('input', { bubbles: true }));", zip_code_field)
    driver.execute_script("arguments[0].blur();", zip_code_field)
    driver.execute_script("angular.element(arguments[0]).triggerHandler('input')", zip_code_field)
    driver.execute_script("angular.element(document.body).scope().$apply();")

    print("✅ Zip Code updated.")

    # Try to Click "Save" Button
    try:
    save_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Save') and not(@disabled)]"))
    )
    driver.execute_script("arguments[0].click();", save_button)
    print("✅ Save button clicked!")
    except Exception:
    print("❌ Save button still disabled!")

    driver.quit()


    The "Save" button does not appear or become clickable when I input text via Selenium. Manually, when I type, the button gets enabled. While doing with Selenium I can see the data being written but no updates in the button. ❓ Question How can I properly trigger the UI update so that the "Save" button appears and becomes enabled?

    Continue reading...

Compartilhe esta Página