2014-10-29 46 views
0

如何使用硒webdriver将数字转换为Excel表格中的字符串 将Excel中的值作为10传递给Excel表格,但在Web应用程序中将其作为10.0输入。 由于事先如何使用硒webdriver将excel表格中的数字转换为字符串

+0

你读入一个字符串从Excel价值? – 2014-10-29 12:19:09

+0

这听起来像通过。您使用什么方法来设置单元格中的值?一些示例代码在这里非常有帮助 – bcar 2014-10-29 12:35:23

+0

im从excel sheet中读取文本值10,但是当它进入webelement时,它将值输入为10.0。我也改变了类别在Excel中的文本。 – Vids 2014-10-29 12:45:45

回答

1

解决方案1 ​​

我希望你使用的是Apache POI从Excel中读取。 在这种情况下,这个样本可以帮助你

CellReference cr = new CellReference("A1"); 
XSSFRow row = sheet.getRow(cr.getRow()); 
XSSFCell cell = row.getCell(cr.getCol()); 
switch (cell.getCellType()) { 
    case XSSFCell.CELL_TYPE_NUMERIC: 
     System.out.println(cell.getRawValue()); 
     break; 
    case XSSFCell.CELL_TYPE_BOOLEAN: 
     System.out.println(cell.getBooleanCellValue()); 
     break; 
    case XSSFCell.CELL_TYPE_STRING: 
     System.out.println(cell.getStringCellValue()); 
     break; 
    default: 
     System.out.println(cell.getRawValue()); 
} 

对于价值1234在Excel中cell.getNumericCellValue()将返回1234.0和cell.getRawValue()将返回1234

解决方案2

如果您不能在读取Excel的地方修改任何内容,请在输入数值前使用Webdriver格式化字符串

public static void main(String[] args) { 
    String input = "1234.0"; 
    try { 
     double parseDouble = Double.parseDouble(input); 
     DecimalFormat df = new DecimalFormat("0"); 
     String formatNumber = df.format(parseDouble); 
     System.out.println(formatNumber); 
    } catch (Exception e) { 
     System.out.println("Input is not a numeric"); 
    } 
} 
+0

感谢共享..我无法奇怪的输入value.soo CodeGo.net,只要我需要改变的值应改变只在Excel表中不是在程序中。 – Vids 2014-10-30 04:26:37

+0

请参阅下面的程序@sachin – Vids 2014-10-30 05:26:15

+0

public static void inttypeIn(String objLocator,String inputValue){ try {findWebElement(objLocator); webElement.sendKeys(inputValue); webElement.sendKeys(Keys.TAB); APP_LOGS.debug( “打字 '” + + inputValue的 “' 中:locatorDescription); } 赶上(例外五) { APP_LOGS.debug(” FAIL: “+ objLocator +” 的描述“定位器 ”+ locatorDescription +“ ':不存在于网页中:“); – Vids 2014-10-30 08:53:42

0

这个输入值应该是字符串,从excel表格中获取整数值,在这个需要将inputvalue改成string.if我改变inputvalue为整数,在这个方法中,sendkeys(inbuild java方法)不接受整数输入值.i在这个方法中不需要奇怪的代码。我需要从excelsheet中读取值并发送给这个方法。

公共静态无效inttypeIn(字符串objLocator,inputValue的字符串){

 try 
     { 
      findWebElement(objLocator); 

     webElement.sendKeys(inputValue); 
     webElement.sendKeys(Keys.TAB); 

     APP_LOGS.debug("Typing '" + inputValue + "' in : " + locatorDescription); 
     } 
     catch (Exception e) 
     { 
      APP_LOGS.debug("FAIL : The locator "+objLocator+" of description '"+locatorDescription+"': does not exists in webpage:"); 
      Reporting.fail("FAIL : The locator '"+objLocator+"' of description '"+locatorDescription+"': does not exists in webpage:"); 
     } 
    } 
相关问题