-2

i want my program to read a single line (which will contain a URL) from excel file. Go to that URL and read next line -->go to the URL untill entire file is read.

import java.io.*;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class readDataFromExcelFile{

 public static void main(String[] args) { 

        WebDriver driver = new FirefoxDriver();

        driver.get("http://www.google.com");

        driver.manage().window().maximize();  

        WebElement searchbox = driver.findElement(By.name("q"));

 try {

  FileInputStream file = new FileInputStream(new File("E://TestData.xlsx")); 
  XSSFWorkbook workbook = new XSSFWorkbook(file);

  XSSFSheet sheet = workbook.getSheetAt(0);

for (int i=1; i <= sheet.getLastRowNum(); i++){

        String keyword = sheet.getRow(i).getCell(0).getStringCellValue();

        searchbox.sendKeys(keyword);

        searchbox.submit();       

        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

}

  file.close();

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

But the above program will get all the /urls at a single time. Can you please tell me what should i modify in the code.

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41

1 Answers1

0

Clear the field before typing more text into it.

sendKeys() does just what it says: It sends a series of "user pressed key X" to the browser. So if the field already contains content, then the new keys are appended.

See: Clear text from textarea with selenium

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820