'Visit links from excel file using selenium web driver

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();
} 


Solution 1:[1]

  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>);

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 HemChe