0

I have an Excel file where all my links are stored in one single column. One Link on each row. For E.g:

www.example.com
www.test.com
www.demo.com

and so on. What I want to do is visit each link and open it in Firefox. Get the link in address bar and compare it with link in excel cell. If both are same them set the string "Pass" in next cell otherwise set string "Fail". How do I do This. Can you please give me a sample code? I am using selenium web driver with java.

Here is what I tried:

try {
    FileInputStream file=new FileInputStream(new File(path));
    FileOutputStream outFile=new FileOutputStream(new File(path));
    HSSFWorkbook workbook=new HSSFWorkbook(file);
    HSSFSheet sheet=workbook.getSheetAt(0);
    HSSFCell cell=null;     
    int s=sheet.getLastRowNum()+1;
    for(int i=0; i<s; i++){
        cell=sheet.getRow(i).getCell(0);
        String url=cell.toString();
        driver.get(url);
        Thread.sleep(10000);
        String urlnew=driver.getCurrentUrl().toString();
        HSSFRow row=sheet.getRow(i);
        HSSFCell cellresult=row.createCell(1);
        if(url==urlnew){
            cellresult.setCellValue("Pass");
        }else{
            cellresult.setCellValue("fail");
        }
        workbook.write(outFile);
    }    
    file.close();  
    outFile.close();
} 
sparkhee93
  • 1,381
  • 3
  • 21
  • 30
Mitesh Hatkar
  • 49
  • 2
  • 5

2 Answers2

0
  1. driver = webdriver.Firefox();
  2. Load the Excel file (you can use this)
  3. In the rows loop, add this code: driver.get(< cell URL>);
Community
  • 1
  • 1
ShacharW
  • 207
  • 1
  • 8
0

In your code you have used the following statement to compare two strings.

if(url==urlnew)

Use the below code. it will work.

if(url.equals(urlnew))

For more information on String comparision, check this link.

HemChe
  • 2,249
  • 1
  • 21
  • 33