最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Selenium not triggering 'Save' button after modifying a placeholder field in AngularJS page - Stack Ove

programmeradmin3浏览0评论

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.webdrivermon.by import By
from selenium.webdrivermon.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(";)

# Login
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "login-email"))).send_keys("[email protected]")
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(";)

# 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.")

# 
发布评论

评论列表(0)

  1. 暂无评论