SendKeys()仅在表单单个输入框失效的技术求助
Hey there, let's figure out why your sendKeys() is only failing on this one input box—super weird that it works everywhere else, especially since your XPath checks out and you can click the element! Here are some practical fixes to try:
1. Ensure Full Focus & Clear Existing Content
Double-clicking might not always select all text in the input box, leaving old values that block your new input. Try a more reliable way to clear and focus:
WebElement decimalPlacesTxtBox = driver.findElement(By.xpath(AreaUtils.XPATH_DECIMAL_PLACES)); // Click to ensure the element is focused first decimalPlacesTxtBox.click(); // Select all existing text using keyboard shortcut decimalPlacesTxtBox.sendKeys(Keys.chord(Keys.CONTROL, "a")); // Delete the selected content decimalPlacesTxtBox.sendKeys(Keys.DELETE); // Now send your value decimalPlacesTxtBox.sendKeys(String.valueOf(newVariable.getDecimalPlaces()));
2. Use JavaScript to Set the Value (Bypass Native Event Issues)
Some modern front-end frameworks (like React, Vue, or Angular) don't always register Selenium's native sendKeys() events. Try directly setting the input's value with JavaScript, plus triggering a change event if needed:
WebElement decimalPlacesTxtBox = driver.findElement(By.xpath(AreaUtils.XPATH_DECIMAL_PLACES)); JavascriptExecutor js = (JavascriptExecutor) driver; // Set the input value js.executeScript("arguments[0].value = arguments[1];", decimalPlacesTxtBox, newVariable.getDecimalPlaces().toString()); // Trigger a change event to let the framework recognize the update js.executeScript("arguments[0].dispatchEvent(new Event('change'));", decimalPlacesTxtBox);
3. Check for Hidden Readonly/Disabled Attributes
Even if you can click the element, it might have dynamic readonly or disabled attributes that prevent input. First, verify the element's state:
WebElement decimalPlacesTxtBox = driver.findElement(By.xpath(AreaUtils.XPATH_DECIMAL_PLACES)); System.out.println("Is element enabled? " + decimalPlacesTxtBox.isEnabled()); System.out.println("Readonly attribute value: " + decimalPlacesTxtBox.getAttribute("readonly"));
If you find these attributes are present, remove them first before inputting:
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].removeAttribute('readonly');", decimalPlacesTxtBox); js.executeScript("arguments[0].removeAttribute('disabled');", decimalPlacesTxtBox); // Proceed with sendKeys or JavaScript value set
4. Add Explicit Wait for Element Interactivity
Sometimes the element is located but not fully ready to accept input. Use an explicit wait to ensure it's clickable before interacting:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); WebElement decimalPlacesTxtBox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(AreaUtils.XPATH_DECIMAL_PLACES))); // Now perform your input steps decimalPlacesTxtBox.click(); decimalPlacesTxtBox.clear(); decimalPlacesTxtBox.sendKeys(String.valueOf(newVariable.getDecimalPlaces()));
Hope one of these fixes gets your input working smoothly!
内容的提问来源于stack exchange,提问作者Nour Elhouda Benyas

