【问题标题】:SendKeys is only putting end of input in text fieldSendKeys 只是将输入的结尾放在文本字段中
【发布时间】:2018-01-18 20:56:34
【问题描述】:

该脚本正在运行一个发货创建表单、定位 xpaths、从 Excel 表输入文本。

有一个字段不断出现错误,当脚本正在编写文本时,它将被删除,并且只显示文本的尾部。 以下是脚本的摘录:

driver // works
    .findElement(By.xpath(prop.getProperty("PostCodexpath")))
    .sendKeys(
        excel
            .getCell(53, i)
            .getContents()
    );

driver // works
    .findElement(By.xpath(prop.getProperty("Cityxpath")))
    .sendKeys(
        excel
            .getCell(51, i)
            .getContents()
    );

driver // fails
    .findElement(By.xpath(prop.getProperty("Statexpath")))
    .sendKeys(
        excel
            .getCell(52, i)
            .getContents()
    );  

对于前两个它工作正常,找到该字段并输入文本,但对于第三行,我需要自动化来找到 xpath,暂停一秒钟(没有显式暂停,没有加载任何内容),然后输入文本。

字段背景: 它具有自动提示功能,根据前 3 个字符建议状态,但这与字段完全分开,可以完全忽略。我无法通过在字段中输入或复制/粘贴来复制它。它仅在自动化运行时发生。

【问题讨论】:

  • 检查一下从excel传过来的值是否正确。
  • 该字段上的绑定事件自动提示打破正常的sendKeys(),您可以使用executeScript()在客户端通过javascript设置值。 driver.executeScript('arguments[0].value=arguments[1]', the field, the value)
  • AutosuggestionsHTML DOM 的一部分,您应该将它们视为不同的WebElement

标签: javascript selenium


【解决方案1】:

您可以尝试将输入更改为 char 数组,然后在字段中逐个字符地输入相同内容,直到获得自动建议,然后从自动建议值中选择第一个值。

    char[] ar = excelvalue.toCharArray();

    String[] str = new String[ar.length];
    WebElement el = driver.findElementBy.Xpath(prop.getProperty("Statexpath")));
    for (int i = 0; i <= ar.length - 1; i++)

    {
        str[i] = String.valueOf(ar[i]);
        el.sendKeys(str[1]);
        Thread.sleep(200);
    }

    el.sendKeys(Keys.DOWN);
    el.sendKeys(Keys.ENTER);

【讨论】:

    【解决方案2】:

    这也可能由于表单设计而发生。例如,数据屏蔽/验证实现可能有问题,这可能会导致这种情况。请围绕此输入评估 JavaScript。

    样本https://github.com/RobinHerbots/Inputmask/issues/505

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2015-06-11
      • 2013-07-20
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      相关资源
      最近更新 更多