Guide on efficiently writing several data entries to Excel using looping in Jxl

I am facing an issue with my code where it is only writing the first value to Excel and not the rest. My goal is to read data from a webpage and write it to an Excel sheet. The current set of code I have works fine, but I need help figuring out how to run this in a loop as I have multiple values to write that are being retrieved from a table.

If anyone could assist with sorting this out, it would be greatly appreciated.

String m1 = (driver.findElement(By.xpath(".//*[@id='dhfdshjfdsfdsf']")).getText());
   System.out.println(m1);
    WritableWorkbook wb = Workbook.createWorkbook(new File("D:\\output_2.xls"));
    writableSheet ws = wb.createSheet("customsheet",1);
    {
    Label label = new Label(0,0,m1);
    ws.addCell(label);
    }
    wb.write();
    wb.close();

Answer №1

I made some improvements to your code so that it can handle multiple values efficiently. Take a look at the updated code below.

WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\output_2.xls"));
    WritableSheet sheet = workbook.createSheet("customsheet", 1);
    int rowSize = sheet.getRows();
    int column = 0;
    
    // If there are 20 values in m1, it will update up to 20 rows
    for (int i = rowSize; i < rowSize + 20; i++) {
        String value = driver.findElement(By.xpath(".//*[@id='dhfdshjfdsfdsf']")).getText();
        System.out.println(value);
        Label label = new Label(column, i, value);
        sheet.addCell(label);
    }
    workbook.write();
    workbook.close();

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

Having trouble retrieving options from a Select element with Selenium

I've been facing some challenges when it comes to choosing options from a dropdown menu using Selenium Webdriver. Here's a snippet of the HTML code: <span id="id14"> <div class="stadium-input-row"> <span class="inputCo ...

Executing a maven project using Jenkins on an Ubuntu system

While the same configuration works fine with Windows 10, I encountered an error when trying to run it with Ubuntu. Click here for the error message displayed while running with Jenkins ...

Is it possible to conceal the chrome browser when using Selenium with C#?

As I work with the Selenium Framework for automating browser tasks, a current challenge I am facing is attempting to conceal the browser during operation. Are there any methods available to hide the browser and perform operations quietly? ...

When utilizing Visual Basic 6 with Selenium, an error may occur stating "Class not registered 80040154."

Having an issue with accessing Selenium using VBA in Excel and VB6. https://i.stack.imgur.com/VBIWD.png Interestingly, I've seen on YouTube that someone managed to use VB6 with Selenium successfully. Private Sub Form_Load() GetData https:// ...

Having trouble running classes using Maven test with the Testng.xml file in the terminal, however, it runs smoothly in Eclipse

While I have been successful in running my solution through the testng suit in the Eclipse console, I am facing difficulties executing the testng.xml file via Maven integrated with Sauce Labs in the terminal. Output received on the terminal: ------------ ...

Is it possible to have Protractor utilize the IEDriverServer.exe that was updated by webdriver-update in the node_modules folder?

Currently, I am in the process of writing acceptance tests for my angular web application project. These tests are executed using protractor and everything is working smoothly on Chrome. However, when attempting to run the tests on Internet Explorer 11, an ...

Setting up the communication ports between the Selenium Server and browsers: A step-by-step guide

I am currently utilizing Selenium Standalone Server version 3.141.59, which can be downloaded from https://www.seleniumhq.org/download While running my code, the Selenium server indicates in its debug messages that it is starting ChromeDriver on port 2820 ...

How can Gson be used to parse multiple data types within a single array?

I'm facing an issue with parsing an API response on Android that contains METAR weather data and error responses within the same "data" array. I'm using Gson for json parsing, which works fine for METAR data but fails when encountering error stri ...

Steps for accessing the alert window after launching the browser using Selenium

Currently, I am using Selenium with Java to access a URL that requires credentials. The URL is https://username:password.example.com/ Even though I have tried using the code snippet below, it only shows the login window rather than an alert window. Please ...

Spring Boot is having trouble identifying the JavaScript files

I've incorporated Spring Boot in my project and I'm working with a JSP file that looks like this: <%@ include file="common/header.jsp" %> <%@ include file="common/navigation.jsp" %> <div class="container"> ...

Data file located at "resources/env1/data.json" is causing an error in the implementation of TestStep on line 5 in the scenarios/jsonformfiller.feature file

Error Oops! It seems like there is an issue with the test step implementation. The error message states that the TestStep implementation could not be found. To resolve this, please provide the implementation or ensure that the 'step.provider.pkg&apo ...

How can Selenium be used to identify an Internet Explorer browser extension?

Can Selenium be used to detect internet explorer browser plugins? For example, if I open a URL on IE and want to check for any installed plugins, is there a way to automate this with selenium? ...

Is there a way to delay script execution in Selenium web driver until the screen has been resized?

Encountering an issue with a specific part of my code: webDriver.manage().window().setSize(new Dimension(width,height)); TimeUnit.MILLISECONDS.sleep(100); webDriver.executeScript("window.scrollTo(0,document.body.scrollHeight);"); The challenge lies in en ...

Spring Jackson - Field "response" is not recognized and cannot be ignored within the context of the program

Trying to convert a jSon String to an Object using Jackson serialization. The jSon string that needs to be converted is: {"response":{"status":1,"count":"90120"}} Here's the Object structure: @JsonRootName("response") @JsonIgnoreProperties(ignoreU ...

Comparing Java's iText library with Node's Puppeteer for converting HTML to PDF documents

Is it better to use iText or Puppeteer when generating PDFs from an HTML page and creating a service in either Node.js or Java? Which one offers superior features and performance? Additionally, does Selenium provide the same PDF generation capabilities as ...

Utilizing Webdriver in C# to loop through elements and add their names to a screenshot array

My Plan: Grab the dropdown menu Find each option in the dropdown menu Click on each option, take a screenshot Progress so far: IWebElement DropDownMenu = driver.FindElement(By.XPath("//*[@id='DropDownMenu']/span")); ...

Error: Reflection cannot be used to define a class

Recently, I've been facing an error while using gulp-angular-protractor for end-to-end testing of my web app. It used to work perfectly before but now I'm encountering this issue. https://i.stack.imgur.com/E7m4i.png I would greatly appreciate a ...

Selenium - enlarging an element fails, but shrinking it is successful

I am currently working on a test script to handle expand and collapse actions on specific collapsible elements within a webpage. You can find the URL where this functionality is being tested at: Here's a snippet of the code from my page object: @Fin ...

Error encountered during execution of For-Each loop using VBA and Selenium?

I encountered an issue with the error message "Object doesn't support this property or method." Here is the code snippet that triggered the error: Dim webs, web As Object Set webs = driverg.findElementByCssSelector("*[class^='title-link id-trac ...

Guide to randomizing a collection of URLs and implementing it with the Webdriver

Looking to extract Href links from a website and shuffle them, then iterate through each line in the list to scrape each webpage using Selenium in Python. I have come across information on how to do this with a notepad file, but I am looking for guidance o ...