I have a requirement where I need to compare web data with Excel, I need code for the same.

  • Can you be more specific? WebElement objects can retrieve manifold of data( seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/…). If you can share a representation of these objects in Excel and what you are referencing on the WebElement objects, we could help. – João Farias Jun 11 at 12:06
  • It is a report in the form of rows and columns, 1st column contains "Type of report(eg. Sales and sales is further divided into monthly and Annual)" & in rows it contains year, we can also download this report by download excel button, I need to compare data present on the webpage with data present on the excel. Also 5-6 columns have some filter criteria in two rows and columns – jay Jun 11 at 12:21
  • Please update your question with this information and post some sample data to give us a better idea of what you are trying to do. – Kate Paulk Jun 11 at 13:41
  1. You can get data from Excel using Apache POI library. Check out Busy Developers' Guide to HSSF and XSSF Features
  2. Once you get the data from Excel you can check its presence at the webpage. Not knowing the details of Excel file and the webpage structure it's impossible to come up with the code, moreover StackExchange is not a code-writing service. A wild-card like XPath expression which looks for all elements containing specific text would be:

  3. Putting everything together:

    FileInputStream excelFile = new FileInputStream(new File("c:/path/to/your/file.xls"));
    Workbook workbook = new HSSFWorkbook(excelFile); // for xls files or XSSFWorkbook for xlsx files
    Sheet sheet1 = workbook.getSheetAt(0);
    Row row1 = (Row) sheet1.iterator().next();
    Cell a1 = row1.getCell(0);
    String valueFromExcel = a1.getStringCellValue();
    ChromeDriver driver = new ChromeDriver();
    List<WebElement> matches = driver.findElementsByXPath("//*[contains(text(),'" + valueFromExcel + "')]");
    if (matches.size() > 0) {
        System.out.println(matches.size() + " elements were found")
    } else {
        System.out.println("No elements with text: " + valueFromExcel + " were found at the page");
  • Thanks for your efforts, I have one question there is no cursor or link is available on webpage, So its possible to find xpath of that and compare with Excel sheet. – jay Jun 12 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.