使用Selenium Python脚本获取网页转换值失败求助
Hey, I’ve dealt with this exact problem before—when manual input works but your Selenium script just gets stuck on a 0 for the converted value. It almost always boils down to missing JavaScript events or not waiting for dynamic updates. Let’s go through the most reliable fixes:
1. Trigger the Required JavaScript Events
Most conversion fields rely on events like input, change, or keyup to trigger the calculation. Selenium’s send_keys() doesn’t always fire these automatically, so you need to manually dispatch them:
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 # Initialize driver (adjust based on your browser) driver = webdriver.Chrome() driver.get("weblink") # Wait for the input field to be interactable amount_input = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Your Amount Placeholder']")) ) # Clear existing value and input your amount amount_input.clear() amount_input.send_keys("150") # Trigger the input event to kick off the conversion driver.execute_script("arguments[0].dispatchEvent(new Event('input'));", amount_input) # If input doesn't work, try the change event instead # driver.execute_script("arguments[0].dispatchEvent(new Event('change'));", amount_input)
2. Wait for the Converted Value to Update
The conversion might happen asynchronously, so don’t grab the value immediately. Use an explicit wait to ensure it’s updated from 0:
# Wait until the converted value is no longer 0 (adjust selector to match your element) converted_element = WebDriverWait(driver, 10).until( lambda d: d.find_element(By.ID, "converted-value").text.strip() != "0" ) # Now get the updated value print("Converted amount:", converted_element.text)
3. Simulate Real User Interaction More Closely
Sometimes mimicking exactly how a human interacts fixes the issue—like clicking the input first, selecting all existing text, and tabbing away:
from selenium.webdriver.common.keys import Keys amount_input.click() # Select all existing text (Ctrl+A) and delete it amount_input.send_keys(Keys.CONTROL, "a") amount_input.send_keys(Keys.DELETE) # Input the new amount amount_input.send_keys("200") # Tab away to trigger focus change events amount_input.send_keys(Keys.TAB)
4. Bypass the Browser: Call the Direct API
If the above don’t work, check if the site uses an API to calculate conversions. Open your browser’s DevTools (F12), go to the Network tab, and watch for requests when you input an amount. You can then call this API directly with requests instead of using Selenium:
import requests # Replace with the actual API endpoint you found in DevTools api_url = "https://example.com/api/calculate-conversion" # Adjust payload to match the request parameters payload = { "amount": 250, "source_currency": "USD", "target_currency": "GBP" } response = requests.post(api_url, json=payload) converted_data = response.json() print("Direct API converted amount:", converted_data["result"])
Start with steps 1 and 2—they solve 90% of these cases. If those fail, move to simulating user interaction or checking for the API.
内容的提问来源于stack exchange,提问作者SIM

