求助:适配WhatsApp新版HTML的Python/JS消息发送程序修改方案
Hey there! I’ve dealt with this exact WhatsApp Web DOM change issue before—nothing’s more frustrating than a working script breaking after a platform update. Since you’re comfortable with Python, JS, and C#, here are tailored solutions for each language to get your Excel-to-WhatsApp messaging back up and running:
Python + Selenium Approach
WhatsApp Web’s current message input area is a contenteditable div (instead of the old span), and the send button uses a consistent data-icon attribute. Here’s how to adapt your script:
- First, wait for the chat input box to load (always use explicit waits instead of
time.sleep()for reliability):
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get("https://web.whatsapp.com") # Wait for QR scan (you can add a manual wait or automate this if needed) input("Scan QR code and press Enter...") # Target the chat input box (contenteditable div with specific aria-label) wait = WebDriverWait(driver, 10) input_box = wait.until(EC.presence_of_element_located( (By.CSS_SELECTOR, 'div[contenteditable="true"][aria-label="Type a message"]') )) # Send your message from Excel (replace with your Excel data fetch logic) message = "Hello from your updated script!" input_box.send_keys(message) # Click the send button (uses data-icon="send") send_button = wait.until(EC.element_to_be_clickable( (By.CSS_SELECTOR, 'span[data-icon="send"]') )) send_button.click()
- Pro tip: If the
aria-labelchanges, you can fall back to locating the input box via its parent container (e.g.,div._2vbn4—but note class names might shift, so prefer aria attributes first).
JavaScript Approach (Puppeteer or Browser Script)
If you prefer JS, Puppeteer is a great headless browser tool for this. Here’s a quick adaptation:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch({ headless: false }); const page = await browser.newPage(); await page.goto('https://web.whatsapp.com'); // Wait for QR scan await page.waitForSelector('div[data-testid="qrcode"]'); console.log('Scan QR code and press Enter in terminal...'); await new Promise(resolve => process.stdin.once('data', resolve)); // Locate input box and send message const inputBox = await page.waitForSelector('div[contenteditable="true"][aria-label="Type a message"]'); await inputBox.type('Hello from JS script!'); // Click send button const sendButton = await page.waitForSelector('span[data-icon="send"]'); await sendButton.click(); // Optional: Close after delay setTimeout(async () => { await browser.close(); }, 5000); })();
- If you’re running this directly in the browser console, you can simplify it to direct DOM queries:
// Find input box and set message document.querySelector('div[contenteditable="true"][aria-label="Type a message"]').innerText = "Your message here"; // Trigger send (simulate click on send button) document.querySelector('span[data-icon="send"]').click();
C# + Selenium Approach
For C# users, the logic mirrors Python—focus on explicit waits and the new selectors:
using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; using System; class WhatsAppSender { static void Main() { IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("https://web.whatsapp.com"); Console.WriteLine("Scan QR code and press Enter..."); Console.ReadLine(); WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); // Locate input box IWebElement inputBox = wait.Until(d => d.FindElement(By.CssSelector("div[contenteditable='true'][aria-label='Type a message']"))); inputBox.SendKeys("Hello from C# script!"); // Click send button IWebElement sendButton = wait.Until(d => d.FindElement(By.CssSelector("span[data-icon='send']"))); sendButton.Click(); // Cleanup driver.Quit(); } }
Key Notes for All Languages
- Avoid hard-coded class names: WhatsApp frequently changes class names (like
_2vbn4), so prioritize attributes likearia-label,data-icon, orcontenteditablewhich are more stable. - Explicit waits are non-negotiable: The page loads dynamically, so using
WebDriverWait(Python/C#) orwaitForSelector(JS) ensures you don’t try to interact with elements before they exist. - Test selectors in DevTools: Right-click the input box/send button in WhatsApp Web, inspect it, and verify your selectors work in the DevTools console before adding them to your script.
内容的提问来源于stack exchange,提问作者Erel Nahum




