【问题标题】:Sendkeys() method in selenium webdriverselenium webdriver 中的 Sendkeys() 方法
【发布时间】:2014-11-26 08:10:52
【问题描述】:

WebElement 类型中的方法sendKeys(CharSequence...) 不适用于参数(double)

wd.findElement(By.id("----")).sendKeys(sheet.getRow(2).getCell(0).getNumericCellValue());

如何将 Excel 中的数字单元格值输入到 sendkeys 方法中,对于字符串值,它工作正常。

现在我正在使用 poi jar 文件

【问题讨论】:

    标签: selenium selenium-webdriver


    【解决方案1】:

    很简单,将数值转换为字符串。像 String str = ""+5;

    对于您的情况,请使用 ""+sheet.getRow(2).getCell(0).getNumericCellValue()

    wd.findElement(By.id("----")).sendKeys(""+sheet.getRow(2).getCell(0).getNumericCellValue());

    如果是数字单元格,apache poi 返回双精度值。要获得准确的双精度值,即没有字符“E”,请将双精度值转换为 BigDecimal 并获取它的字符串表示形式,如下所示

    Double d = 1.234567E8;
    String str1 = new BigDecimal(d).toString();
    System.out.println(str1);
    

    输出:123456700 就是这样

    【讨论】:

    • 它正在将数值转换为 1.234567E8
    • 使用 BigDecimal 类,参考下面的示例代码并在您的 ide 中执行。双 d = 1.234567E8;字符串 str1 = new BigDecimal(d).toString(); System.out.println(str1);
    • 坦克为你的支持
    • 我们如何从 Excel 获取日期值到 webdriver 中的 Sendkeys() 方法
    【解决方案2】:

    因为“Sendkeys()”只接受字符串作为参数,所以如果你想将一个数值传递给sendkeys,你必须把它转换成字符串。我将举一个例子来详细解释这一点。

    假设你从 excel 中得到“1455”,你想把它传递给 sendkeys,然后像这样将它转换成字符串:

    String num = Double.toString("1455");
    webElement.SendKeys(num);
    

    【讨论】:

      【解决方案3】:

      SendKeys 需要传递一个 String 对象,但您传递的是一个数值。

      试试这个:

      WebElement element = null;
      try {
          element = wd.findElement(By.id("----"));
          element.sendKeys(sheet.getRow(2).getCell(0).getStringCellValue());
          );
      } catch (Exception e) {
          /* this is thrown if the cell contains a formula that isn't a "string Formula" */
      }
      

      请注意,我已将 getNumericCellValue() 更改为 getStringCellValue()

      【讨论】:

        猜你喜欢
        • 2012-10-15
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 1970-01-01
        • 2023-02-09
        • 2021-11-21
        • 2013-10-10
        相关资源
        最近更新 更多