【问题标题】:Selenium Webdriver How to select records from table by fetching Excel InputSelenium Webdriver 如何通过获取 Excel 输入从表中选择记录
【发布时间】:2014-12-11 14:46:57
【问题描述】:

我正在为以下情况而苦苦挣扎。

  1. 应用显示的100个供应商的记录在一张表中有ID、公司名称和订阅名称三列。
  2. 我想从我的 Excel 表中输入公司名称“xyz”,并使用该输入我必须单击订阅名称详细信息链接,以便应用程序导航到我的下一页。

我创建的示例代码如下:

`public static void main(String[] args) throws BiffException, IOException, Exception { WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); //工作簿位置 工作簿 wBook = Workbook.getWorkbook(new File("C:\Users\amit.bhagwat\Documents\TestData\SampleData.xls")); //获取工作表 jxl.Sheet Sheet = wBook.getSheet(0);

    //loop
    for(int i=1; i<Sheet.getRows(); i++)

        {
    driver.get("http://206.132.42.243/Web");
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    driver.findElement(By.xpath("//input[@id='UserName']")).sendKeys(Sheet.getCell(0, i).getContents());
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    driver.findElement(By.xpath("//input[@id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
    driver.findElement(By.xpath("//input[@id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
    Thread.sleep(40);
    driver.findElement(By.xpath("//input[@name='Login']")).click();
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    driver.findElement(By.xpath("//a[contains(text(),'Task')]")).click();
    driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    driver.findElement(By.xpath("//a[contains(text(),'Data Checking')]")).click();
jxl.Sheet Sheet2 = wBook.getSheet(0); 
    WebElement kancheck = driver.findElement(By.name("Grant & Brown"));
    kancheck.click();
    System.out.println(kancheck.isSelected());
    driver.findElement(By.xpath("//a[contains(text(),'Data Checking')]")).sendKeys(Sheet2.getCell(1, i).getContents());
    Thread.sleep(40);` enter code here

【问题讨论】:

    标签: excel selenium-webdriver


    【解决方案1】:

    据我所知,您正在尝试从远程位置读取文件,然后从中读取信息。如果您可以使用Apache POI 库在运行时读取内容,这将是一个好习惯。

    在我的项目中,我使用Apache POI 库从 Excel 工作表中读取所有内容来设置变量的值。这是关于我如何实现它的代码 sn-p。希望这将指导您找到正确的解决方案。 :)

        public void readExcelDoc() throws FileNotFoundException, IOException
    {
    
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("excelDoc//scripts.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = null;
        HSSFCell cell = null;
    
        int rows = 0; // No of rows
        //      rows = sheet.getPhysicalNumberOfRows();
        rows = sheet.getLastRowNum();
    
        int cols = 2; // No of columns
        int tmp = 0;
    
        // This trick ensures that we get the data properly even if it doesn't start from first few rows
        for(int i = 0; i < 10 || i < rows; i++) {
            row = sheet.getRow(i);
            if(row != null) {
    
                tmp = sheet.getRow(i).getPhysicalNumberOfCells();
    
                if(tmp > cols) cols = tmp;
            }
    
        }
        int testRowNo = 0;
        String rowName = "Test Name";
        String columnValue = " ";
        //Iterate through Row and columns here. Excluding 1st row for title names
        for(int r = 1; r <= rows; r++) {
    
            row = sheet.getRow(r);
            if(row != null) {
                //Browse through columns using c
                for(int c = 0; c < cols; c++) {                     
                    if(c==0) //Only taking data from Cell 0; Ignoring any other inputs
                    {           
                        cell = row.getCell((short)c);
                        try
                        {
                            if(cell.getStringCellValue().contains(rowName)) 
                            {           
                                testRowNo =row.getRowNum();
                            }   
    
                        if(testRowNo > 0 )
                        {
                            if(cell.getColumnIndex() == 0 && row.getRowNum() > testRowNo && cell.getStringCellValue().length() !=0)
                            {
                                try{
                                    String cellValue = cell.getStringCellValue().toLowerCase();
                                    //System.out.println(cellValue);
                                    scriptType.add(cellValue);
                                }
                                catch(IllegalStateException e)
                                {
                                    e.printStackTrace();
                                    scriptType.add(cell.getStringCellValue());
                                }
                            }
                        }
                        }
                        catch(NullPointerException e)
                        {
    
                        }
    
                    }
                    if(c==1)
                    {
                        cell = row.getCell((short)c); //this sets the column number
                        if(testRowNo == 0)
                        {       
                            try{
                                String cellValue = cell.getStringCellValue();
                                //System.out.println(cellValue);
                                columnValue = cellValue;
                            }
                            catch(IllegalStateException e)
                            {                           
                                String cellValue = cell.toString();
                                columnValue = cellValue;                                            
                            }
                            catch(NullPointerException e)
                            {
                                String cellValue = nodata;
                                columnValue = cellValue;
                            }
                        }
                    }
                    if(c==2)
                    {
                        cell = row.getCell((short)c); //this sets the column number
                        if(testRowNo == 0)
                        {
    
                            try{
                                String cellValue = cell.getStringCellValue();
                                //System.out.println(cellValue);
                                inputParameters.put(cellValue, columnValue);
                            }
                            catch(IllegalStateException e)
                            {                           
                                String cellValue = cell.toString();
                                inputParameters.put(cellValue, columnValue);                                        
                            }
                            catch(NullPointerException e)
                            {
                                String cellValue = nodata;
                                inputParameters.put(cellValue, columnValue);
                            }
                        }
                    }
    
                }
            }
        }
        System.out.println("---------The parameters set from excel are :  ---------");
        @SuppressWarnings("rawtypes")
        Iterator iterator = inputParameters.keySet().iterator();  
    
        while (iterator.hasNext()) {  
            String key = iterator.next().toString();  
            String value = inputParameters.get(key).toString();  
            System.out.println(key + " : " + value);  
        } 
    }
    

    【讨论】:

    • Hi Fahim 感谢您的回复,但我能够从 excel 表中读取信息,仅网络应用程序上的问题我无法选择我提到的任何记录的那些链接(订阅详细信息)bcoz 每条记录都有相同的链接,因此如何选择公司名称为“ABC”的第 29 号记录的链接。在 excel 表中,我只提供了公司名称,所以 selenium 应该从我的 excel 表中获取这些公司名称作为输入值,然后它会在应用程序中选择订阅链接,这是一个期望但不能走这么远......需要小提示只有休息我会做的......
    • 如果这次我猜对了,那么您就可以从 excel 表中读取所有内容。然后你应该做的是遍历excel表中的公司名称列表,并一步将其保存在一个数组中。然后,将 array[index] 发送到您的 .sendKeys 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2013-12-11
    • 2017-08-12
    • 2012-10-23
    • 2019-01-11
    相关资源
    最近更新 更多