You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium sendKeys命令异常求助:空格后内容无法输入

问题:Selenium输入带空格的全名时空格后内容丢失,出现大量空白

我在自动化一个需要填写个人信息的网站,「申请人姓名」字段需输入包含空格的全名,但使用Selenium的sendKeys命令时,空格后的姓氏无法输入,反而出现大量空白。尝试直接输入带空格的字符串或拆分输入(先输名字+Keys.SPACE+姓氏)均无效,相邻文本框却可正常工作。

我的代码(Eclipse中编写)

package automation;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class residence {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        /*Store the current window handle */
        String parent_handle = driver.getWindowHandle();
        driver.get("https://serviceonline.bihar.gov.in/resources/homePage/10/loginEnglish.htm");
        driver.findElement(By.xpath("//label[contains(text(),'General')]")).click();
        driver.findElement(By.xpath("//p[contains(text(),'Residential')]")).click();
        driver.findElement(By.xpath("//div[@id='collapseOneOne']/div/p/a")).click();
        
        for(String winHandle:driver.getWindowHandles()){
            if(!parent_handle.equals(winHandle))
                driver.switchTo().window(winHandle);
        }
        
        /* Write Gender accordingly.Default is Male(M).(F) and (T)*/
        char gender='M';
        WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(30));
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[@for='17290_1']/input")));
        if(gender=='M')
        {
            driver.findElement(By.xpath("//input[@id='17290_1']")).click();
        }
        else if(gender=='F')
        {
            driver.findElement(By.xpath("//input[@id='17290_2']")).click();
        }
        else
        {
            driver.findElement(By.xpath("//input[@id='17290_3']")).click();
        }
        
        WebElement obj=driver.findElement(By.xpath("//input[@name='78250']"));
        
        /* I have also tried obj.sendKeys("Sumit Kumar") , this is also not working*/
        
        obj.clear();
        obj.sendKeys("Sumit",Keys.SPACE,"Kumar");
        obj = driver.findElement(By.xpath("//input[@name='17287']"));
        obj.clear();
        obj.sendKeys("Name in Hindi with space ");
    }  

}  

错误截图

输入空格后异常的截图


解决办法

1. 使用JavaScript注入输入

部分网站的输入框有自定义前端事件监听,Selenium原生sendKeys可能触发异常,改用JS直接设置输入框value值更稳定:

// 导入JavascriptExecutor包
import org.openqa.selenium.JavascriptExecutor;

// 替换原有的sendKeys代码
WebElement nameInput = driver.findElement(By.xpath("//input[@name='78250']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value = 'Sumit Kumar';", nameInput);
// 可选:触发change事件,确保前端脚本识别输入
js.executeScript("arguments[0].dispatchEvent(new Event('change'));", nameInput);

2. 排查前端干扰因素

  • 手动在浏览器目标输入框输入带空格的全名,确认是否是网站自身前端逻辑问题(比如自动格式化脚本)。
  • 检查输入框HTML属性,看是否有maxlength、pattern限制,或绑定了input、keydown等事件。

3. 确认元素定位准确性

用浏览器开发者工具(F12)验证//input[@name='78250']是否唯一指向目标输入框,避免定位到隐藏的同名元素。

4. 添加输入验证

输入后获取输入框实际value值,排查输入是否成功或被前端修改:

String actualValue = nameInput.getAttribute("value");
System.out.println("输入框实际值:" + actualValue);

内容的提问来源于stack exchange,提问作者user14198433

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 14:25:24