【问题标题】:Unable to enter chars in the textfield using selenium sendKeys无法使用 selenium sendKeys 在文本字段中输入字符
【发布时间】:2019-10-02 21:32:53
【问题描述】:

我正在尝试在文本字段中输入字符,但它不起作用。下面我提到了两段代码。 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')");

【问题讨论】:

  • 发布在该表单上运行的标记和任何脚本。有时它可能是一个过于复杂的脚本,跟不上打字。 (如果是这种情况,放慢打字速度会有所帮助......)
  • 提供的元素 html。顺便说一句,我尝试在输入一个字符之前和之后等待 5 秒钟,但我仍然看不到它的输入。
  • 尝试不点击或清除...两者都不需要。
  • 同样的问题。
  • 发布完整标记...可能是 iframe 或缺少 WebDriverWait 或类似的东西。您是否发现任何异常情况?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

所需的元素是动态元素,因此要在元素上调用sendKeys(),您必须为elementToBeClickable() 诱导WebDriverWait,您可以使用以下Locator Strategies 之一:

  • 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");
    

【讨论】:

    【解决方案2】:

    尝试使用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;
    

    【讨论】:

      【解决方案3】:

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

      1. 尝试先单击文本框,然后调用 sendKeys()。
      2. 当输入事件到达太快时,Angular 无法处理它们。 作为一种解决方法,您需要发送单个字符,每个字符之间有一小段延迟。

      Selenium sendKeys are not sending all characters

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

      【讨论】:

        猜你喜欢
        • 2018-07-24
        • 1970-01-01
        • 2016-03-27
        • 1970-01-01
        • 2018-08-08
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多