为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用 selenium sendKeys 在文本字段中输入字符

无法使用 selenium sendKeys 在文本字段中输入字符

猛跑小猪 2023-09-20 16:16:57
我正在尝试在文本字段中输入字符,但它不起作用。下面我提到了2段代码。JS 根本不起作用。在第一段代码中,单击有效,但其他步骤无效。Xpath 是正确的,因为 click 正在处理该问题。    util.driver.findElement(By.xpath("//input[@id='input-1']")).click();    util.driver.findElement(By.xpath("//input[@id='input-1']")).clear();    util.driver.findElement(By.xpath("//input[@id='input-1']")).sendKeys("hjgfjg");JavascriptExecutor js = (JavascriptExecutor) util.driver;js.executeScript("document.getElementByXpath('//input[@id='input-1']').value = 'TEST')");
查看完整描述

3 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

所需的元素是动态元素,因此要调用sendKeys()该元素,您必须引发WebDriverWait ,并且elementToBeClickable()可以使用以下任一定位器策略:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.slds-input[id^='input-'][aria-describedby^='help-message-']"))).sendKeys("hjgfjg");
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='slds-input' and starts-with(@id, 'input-')][starts-with(@aria-describedby, 'help-message-')]"))).sendKeys("hjgfjg");



查看完整回答
反对 回复 2023-09-20
?
浮云间

TA贡献1829条经验 获得超4个赞

尝试使用Actions:


WebElement input = util.driver.findElement(By.xpath("//input[@id='input-1']"));

Actions action = new Actions(util.driver);

action.moveToElement(input).click().sendKeys("test").build().perform();

导入后:


import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;


查看完整回答
反对 回复 2023-09-20
?
撒科打诨

TA贡献1934条经验 获得超2个赞

也许您可以尝试以下解决方法:

  1. 尝试先单击文本框,然后调用 sendKeys()。

  2. 当输入事件到达太快时,Angular 无法处理它们。作为解决方法,您需要发送单个字符,并且每个字符之间有较小的延迟。

Selenium sendKeys 不发送所有字符

  1. 您可以尝试使用 Actions 类。


查看完整回答
反对 回复 2023-09-20
  • 3 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信