首页 > 解决方案 > 如何在浏览器中从excel中一一读取和运行多个URL

问题描述

请建议我一些代码,用于从 selenium 中的 excel 文件中读取和执行大约 300 个 URL。

我做了这样的事情:

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Jatin\\Downloads\\chromedriver_win32\\chromedriver.exe");

         WebDriver driver = new ChromeDriver();

         driver.get("http://example.com/");

         List<WebElement> getLinks = driver.findElements(By.tagName("a"));  
         // to get the list of urls from the website 
         System.out.println(getLinks.size());


         //fetch data from excel
         try {
                File excel = new File("C:\\Users\\Jatin\\Documents\\Output.xlsx");
                FileInputStream fis = new FileInputStream(excel);
                XSSFWorkbook book = new XSSFWorkbook(fis);
                XSSFSheet sheet = book.getSheetAt(0);

                Iterator<Row> itr = sheet.iterator();

                // Iterating over Excel file in Java
                while (itr.hasNext()) {
                    Row row = itr.next();

                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {

                        Cell cell = cellIterator.next();

                        switch (cell.getCellType()) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                            break;
                        default:


                        }
                    }
                    System.out.println("");}
                }catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
    }

标签: javaseleniumselenium-webdriverautomationautomated-tests

解决方案


推荐阅读