Selenium/Webdriver code to run after reading spreadsheet with Apache Poi

Asked

Viewed 1,512 times

1

My test code using Selenium/Webdriver stops running after reading Apache Poi data.

Can anyone tell me why?

//poi
WebElement searchbox = driver.findElement(By.name("simcard"));

try {

    FileInputStream file = new FileInputStream(new File("C:\\paulo.xls")); 
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    HSSFSheet sheet = workbook.getSheetAt(0);

    for (int i=1; i <= sheet.getLastRowNum(); i++){
        String simcard = sheet.getRow(i).getCell(0).getStringCellValue();
        searchbox.sendKeys(simcard);                
        searchbox.submit();
        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
    }

    workbook.close();
    file.close();

} catch (FileNotFoundException fnfe) {
    fnfe.printStackTrace();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

The next step:

driver.findElement(By.xpath("/html/body/table/tbody/tr[4]/td/div/form/table/tbody/tr[2]/td/center/table/tbody/tr[19]/td/a[1]/img")).click();
  • Hello Paulo! What is "Data Driven" in your code? Do you really want to search each line of the spreadsheet and wait 10 seconds between each search? The component searchbox should receive both the text to be searched as well as the submit? Wouldn’t have a button?

  • Luiz, I’m new to programming, I copied this code and tried to adapt in my... I really want him to open the spreadsheet, take a value and put it in the input, do not need to wait 10 seconds and do not scan the spreadsheet... Can you reconstruct this code snippet for me, please?

1 answer

2


Based on my understanding of what you want to do and some assumptions, this would be the code that meets the goal:

//referência ao campo de busca
WebElement searchbox = driver.findElement(By.name("simcard"));

try {

    //carrega arquivo com planilha
    FileInputStream file = new FileInputStream(new File("C:\\paulo.xls")); 
    HSSFWorkbook workbook = new HSSFWorkbook(file);

    //recupera primeira planilha (aba)
    HSSFSheet sheet = workbook.getSheetAt(0);
    //pega o valor da primeira célula (A1)
    String simcard = sheet.getRow(0).getCell(0).getStringCellValue();

    //fecha planilha e arquivo (não precisa fechar os dois)
    workbook.close();

    //envia o valor da célula para o campo
    searchbox.sendKeys(simcard);

    //envia o formulário (precisa ser um form normal, se a tela usar JavaScript pode não funcionar)
    searchbox.submit();

    //aguarda 10 segundos (esperar carregar os dados, talvez)
    driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

} catch (Exception e) {
    e.printStackTrace();
}

//próximo passo aqui

Remarks:

  • No need to close both workbook and file. When closing the first one, the second one is automatically closed.
  • You don’t need a loop to catch the value. And the first line starts with 0 and not with 1.
  • You can close the spreadsheet right after reading the value.
  • If it still doesn’t work, check the console or log to see if an exception is occurring. In this case, edit your question and add the error stack.
  • If the program crashes it may be some Webdriver problem with your browser. In this case, update the Selenium/Webdriver version, try another browser, make sure there is no Javascript running something on the page that can cause the crash.
  • Luiz, with the previous code he filled the data but did not pass to the next step, but with this code you passed He does not fill the field simcard, with the value of the spreadsheet, and does not execute the next step also.

  • 1

    The code is probably not finding the value in the worksheet. Check if the value to be searched is in cell A1 of the worksheet. Can you debug code? Here’s how to debug code step by step to see what happens here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.