无法使用 sendKeys 方法在文本框字段中键入。 xpath 工作正常,因为当我尝试单击文本框时它工作

Unable to type in Text box field using sendKeys method. The xpath is working fine, since when i tried to click on textbox it works

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        WebElement External = driver.findElement(By.xpath("//input [@id ='External_Reviewer']"));
        External.click();
        External.sendKeys("kevin");

文本框是带有人员选择器字段的自动建议。除了仅使用 "sendKeys" 方法之外,还有其他方法吗? 提前致谢。

您可以使用 JavaScript 执行器

External =driver.findElement(By.xpath("//input [@id ='External_Reviewer']"));
driver.executeScript("arguments[0].setAttribute('value', '"yourvalue"')", External);

Selenium 执行得如此之快。您只需要在执行点击后添加一些等待,然后使用 sendKeys()。

所以,它会像:

External.click();
Thread.sleep(2000);
External.sendKeys("kevin");

如果sendKeys()不工作,你可以尝试Actionsclass。请尝试使用以下功能:

public void typeTextIntoSpecialInput(WebElement elem, String input) {
    Actions actions = new Actions(driver);
    this.moveToElement(elem, true);
    actions.sendKeys(input);
    actions.build().perform();
}

希望对你有所帮助。

我遇到过类似的问题,在发送密钥之前点击文本框对我有用