Review all the information in the Excel spreadsheet

I aim to extract data from the initial row, execute an operation on it, record the outcome, then proceed to the subsequent rows until all the data in the Excel file has been processed.

Here are some queries I have:

  • Why are the iterators not functioning as expected?
  • Why isn't Selenium's driver.findElement performing its intended task?

Primary Program

  @Test
    public void test() throws Exception {
        String filePath = "default.xlsx";
        DataHelper.setExcelFile(filePath, "sheet1");
        XSSFRow row;
        XSSFCell cell;
        boolean flag = true;
        XSSFSheet sheet = ExcelWSheet;
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            row = (XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            List <String> obj = new ArrayList<String>();
            while (cells.hasNext()) {
                cell = (XSSFCell) cells.next();
                String cellData = cell.getStringCellValue();
                if(flag)
                {
                    driver.findElement(By.id("text")).sendKeys(cellData);
                    driver.findElement(By.id("text")).click();
                    driver.findElement(By.id("text")).clear;
                }
            }
        }   
    }

DataHelper.java

public class DataHelper {
    public static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell cell;
    private static String filePath;

    public static void setExcelFile(String filePath, String SheetName) throws Exception {
        path = filePath;
        FileInputStream ExcelFile = new FileInputStream(filePath);
        ExcelWBook = new XSSFWorkbook(ExcelFile);
        ExcelWSheet = ExcelWBook.getSheet(SheetName);

    }

}

Excel Data

Email, Password

hello, 123

hello1, 123

Answer №1

After streamlining the code (addressing issues with excessive use of static methods, etc.), the iterators now function smoothly. Take a look at the revised code snippet below.

As for the Selenium calls, I'm not well-versed in that area to offer assistance. My suggestion is to divide your concerns into separate questions for better clarity. One important point to note is the need to explicitly save the workbook to see any modifications reflected in the actual file. If this is your objective, there are numerous easy-to-understand examples readily available for reference.

public void executeTest() throws Exception {
    String filePath = "default.xlsx";
    DataHelper dataHelper = new DataHelper(filePath, "sheet1");
    XSSFSheet sheet = dataHelper.getExcelWSheet();
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        Iterator<Cell> cells = row.cellIterator();
        while (cells.hasNext()) {
            Cell cell = cells.next();
            String cellValue = cell.getStringCellValue();
            if(flag) {
                driver.findElement(By.id("text")).sendKeys(cellValue);
                driver.findElement(By.id("text")).click();
                driver.findElement(By.id("text")).clear;
            }
        }
    }
}

public class DataHelper {
    private XSSFSheet excelWSheet;
    private XSSFWorkbook excelWBook;

    public DataHelper(String path, String sheetName) throws Exception {
        FileInputStream excelFile = new FileInputStream(path);
        excelWBook = new XSSFWorkbook(excelFile);
        excelWSheet = excelWBook.getSheet(sheetName);
    }

    public XSSFSheet getExcelWSheet() {
        return excelWSheet;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the image information from a specified element in the Document Object Model

Is it possible to extract the image data of a DOM element using standard JavaScript or browser extensions? I'm considering scenarios where: Creating an offscreen DOM element Populating it with dynamically styled CSS content Retrieving its image dat ...

Pass the code from the webpage to the PHP script

On my website, I have implemented a feature using JavaScript that dynamically changes the page content. Now, I am looking for a way to send this updated content to a PHP script. How can I achieve this functionality? ...

When attempting to showcase array information in React, there seems to be an issue with the output

After printing console.log(response.data), the following output is displayed in the console. https://i.sstatic.net/LLmDG.png A hook was created as follows: const [result,setResult] = useState([]); The API output was then assigned to the hook with: setRe ...

Difficulties arising when trying to convert latitude and longitude coordinates to xyz for camera rotation within Three.js

Currently, I am working on a JavaScript application that allows users to design their own closet. My goal is to enable smooth rotation of the closet without changing the distance from the camera to the closet. While it would be simple to rotate the object ...

Choose a new file in the file selection window while the program is running

While working with Selenium web driver, I encountered a challenge when dealing with the File browse window. After trying various approaches, I eventually found a solution using AutoIt. Below is the script: @Test public void test() throws InterruptedE ...

Which five key JavaScript concepts should I grasp in order to excel as an AngularJS developer?

Coming from a background of server-side coding, I am delving into the world of AngularJS now. This means I need to have a solid grasp of JavaScript before diving in. If I don't have enough time to master JavaScript completely at the moment, what are ...

Making a Highcharts Pie Chart Legend Embedded Within a Table

Looking for a way to incorporate a Highcharts legend into a table layout? I am aiming to design the legend for my Highcharts pie chart with alternating background colors and custom borders for each element. Although I have styled the legend, I am struggli ...

Ensure that you wait for all asynchronous $http requests to finish in AngularJS before continuing

I am facing a challenge with a page that generates a varying number of $http requests based on the length of a certain variable. I aim to update the scope with the data only after all requests have been completed. Without relying on jQuery for this project ...

How does the performance contrast between "skip if condition" and "immediately return"?

Do you know if there is a performance variance between these two functions? function x() { var x = false; if(x == true) { ... Numerous lines, like 1 million ... } } function y() { var x = false; if (x != true) { retu ...

Guide on how to import or merge JavaScript files depending on their references

As I work on my MVC 6 app, I am exploring a new approach to replacing the older js/css bundling & minification system. My goal is to generate a single javascript file that can be easily referenced in my HTML. However, this javascript file needs to be speci ...

Code snippet in Java to retrieve the time difference between two timestamps stored in an ArrayList

String sql = "SELECT endtime FROM user_req"; ResultSet rs = stmt.executeQuery(sql); ArrayList<Timestamp> list1= new ArrayList<Timestamp>(); while (rs.next()) { list1.add(rs.getTimestamp("endtime")); } System.out.println("\n the requ ...

What is the best way to implement the addMore event in my custom slot components when working with Vue Formulate?

I need help customizing the 'add more' button for group repeatable fields in Vue Formulate. I have created a custom slot component that is functioning correctly, but I am struggling to determine the click event needed to add another field when th ...

Animation does not occur after the stop() function is executed

My goal is to create a functionality where, upon moving back to the grey content box after leaving the button, the slideUp animation stops and the content slides down again. This works seamlessly with jQuery 1.x (edge), but when I switch to jQuery 1.10, th ...

How come the pop-up isn't displaying in the middle of the screen?

I have encountered a positioning issue while using a semantic modal with Angular. The problem arises when I try to display the modal as it does not appear in the correct position. https://i.sstatic.net/o033E.png Below is the code snippet from my HTML fil ...

How can I customize the styling of an SVG pseudo element using Font Awesome 5?

I've implemented font awesome 5 pseudo elements to attach an :after tag to my element as shown below: &:after { content: "\f068"; font-weight:400; color:$brandRed; float:right; font-family: "Font Awesome 5 Pro"; } The c ...

Is there a method to programmatically clear cache in an Angular 7 application?

I am facing an issue with my component that lazy loads images. The first time the page loads, the images are displayed using lazy loading. However, if I refresh, reload, or close and reopen the tab, the images are pre-loaded from the cache. Is there a wa ...

Timeout from WebDriverWait does not occur until the condition specified in wait.until becomes true

I'm facing an issue with my Selenium script that handles the upload of multiple zip files. These files vary in size and duration, leading to different outcomes on the webpage such as displaying errors, showing success messages, or simply continuing th ...

What is the best way to convert the value of "range" to floating point numbers in JavaScript?

Why is it that when I input a float number, like "3.4567890", as the value in an input field, it automatically gets converted to type 'int'? This conversion is not desired, as I want the value to remain as a 'number' or 'float&apos ...

implement an angular directive to apply a CSS element

I am utilizing AngularJS and ng-repeat to populate a dynamic list of studies. This list has the capability to toggle into child elements of each item, creating an accordion-style toggle list that can go up to three levels deep for each list item. I am curr ...

Is there a way to retrieve data from three different Bookshelf.js models in a single query?

Three tables are in my database: User, UserDrink, and VenueDrink Here is a preview of the sample data: User id | name | gender | -------------------- 1 | John | male | 2 | Jane | female | UserDrink id | count | user_id | venue_drink_id 1 | 3 | ...