如何用JavaScript检测IsatPhone短信发送后的成功响应
Got it, let's figure out how to detect if that SMS send was successful since the page isn't updating its HTML with the "Thank you!" message you were hoping to check. The key here is that clicking SEND is probably triggering a background API request—so we can monitor that request and its response to confirm success. Here's how to do this with Selenium (the go-to tool for browser automation):
Step 1: Set Up Selenium with Network Logging
First, we need to configure Selenium to capture browser performance logs, which will let us see all the network requests happening in the background:
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Enable performance logging to track network requests chrome_options = Options() chrome_options.set_capability("goog:loggingPrefs", {"performance": "ALL"}) # Launch Chrome with these options driver = webdriver.Chrome(options=chrome_options) driver.get("http://connect.inmarsat.com/Services/Land/IsatPhone/SMS/sms.html")
Step 2: Fill Out the Form Fields
Next, locate and populate the phone number, email, and message fields. I'm guessing the IDs follow the isatPhone_ prefix from the snippet you shared—adjust these if the actual IDs differ (you can check via your browser's dev tools):
from selenium.webdriver.common.by import By # Fill phone number phone_field = driver.find_element(By.ID, "isatPhone_phoneNumber") phone_field.send_keys("1234567") # Fill email email_field = driver.find_element(By.ID, "isatPhone_email") email_field.send_keys("test@test.com") # Fill message content message_field = driver.find_element(By.ID, "isatPhone_message") message_field.send_keys("Your test message goes here")
Step 3: Click SEND and Monitor the API Request
After clicking the SEND button, we'll sift through the performance logs to find the SMS submission request, then check its response status and content to confirm success:
import json import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Click the SEND button send_button = driver.find_element(By.ID, "isatPhone_sendButton") send_button.click() # Wait a few seconds for the request to complete (or use WebDriverWait for reliability) time.sleep(3) # Alternative: Wait for the button to become unclickable (adjust condition as needed) # WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "isatPhone_sendButton")) is False) sms_sent_successfully = False # Iterate through performance logs to find the SMS request for log_entry in driver.get_log("performance"): log_data = json.loads(log_entry["message"])["message"] # Look for the request that sends the SMS (adjust the URL pattern to match the actual endpoint) if "Network.requestWillBeSent" in log_data["method"] and "sms" in log_data["params"]["request"]["url"]: request_id = log_data["params"]["requestId"] # Find the corresponding response for this request for response_entry in driver.get_log("performance"): response_data = json.loads(response_entry["message"])["message"] if "Network.responseReceived" in response_data["method"] and response_data["params"]["requestId"] == request_id: # Check if the response status is 200 (successful) if response_data["params"]["response"]["status"] == 200: # Optional: Fetch the response body to look for success keywords response_body = driver.execute_script(""" return fetch('%s').then(response => response.text()); """ % log_data["params"]["request"]["url"]) if "success" in response_body.lower() or "thank you" in response_body.lower(): sms_sent_successfully = True break if sms_sent_successfully: break # Output the result if sms_sent_successfully: print("✅ SMS sent successfully!") else: print("❌ SMS send failed or request not detected.") # Clean up driver.quit()
Alternative: Check for JavaScript Success Flags
If the page uses a JavaScript variable or custom event to track success (instead of a visible message), you can directly query the browser's JS context:
# After clicking SEND, check for a JS success flag (adjust variable name to match the page's code) success_status = driver.execute_script("return window.isatPhoneSMSendSuccess;") if success_status: print("✅ SMS sent successfully!") else: print("❌ SMS send failed.")
Important Notes:
- Element IDs: Double-check the actual IDs of form fields and the SEND button using your browser's dev tools (right-click → Inspect).
- Request URL Pattern: Use your browser's Network tab (in dev tools) to see exactly what URL the SEND button hits—adjust the
smscheck in the code to match that endpoint. - Wait Conditions: Replace
time.sleep()withWebDriverWaitfor more reliable tests (it waits until a specific condition is met instead of a fixed time).
内容的提问来源于stack exchange,提问作者Dschubba

