0

Below is my code.Whne i input url from excel most of the time it shows org.openqa.selenium.ElementNotVisibleException: Element is not currently visible error. For site like www.travelocity.com it shows after clicking 7 8 links but for www.google.com it shows error from starting.

    package test;



import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Linktestparam {
public static void main(  String[] args ) throws Exception{

Properties prop = new Properties();
FileInputStream f = new FileInputStream("C:\\Documents and Settings\\bibekananda.sarangi\\workspace\\test\\src\\excelPath");
prop.load(f);
String[][] steps ;
steps = excelRead(prop.getProperty("Linkpath"));

int totallink;

for(int j = 1; j <= steps.length ; j++){
//System.out.println("no of links in " + steps[j][0] + "is" + totallink);   
totallink = linktest(steps[j][0]);

} 
}
public static  int linktest(String url) throws Exception{
WebDriver driver = new FirefoxDriver(); 
driver.navigate().to(url);
Thread.sleep(12000);
//WebDriverWait wait = new WebDriverWait(driver,60);
//wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("gsr")));

List<WebElement> alllinkspresent=driver.findElements(By.tagName("a"));
int totallink = driver.findElements(By.tagName("a")).size();

System.out.println("no of links in " + totallink);

for (int i = 0; i < totallink; i++) 
    {
    int LastRow = i;  
     driver.findElements(By.tagName("a")).get(i).getText();
     driver.findElements(By.tagName("a")).get(i).click();

     System.out.println("LastRow value is" + LastRow);

     Thread.sleep(18000);
     String pagetitle = driver.getTitle();
     System.out.println(pagetitle);
     String urltext=driver.getCurrentUrl();
     System.out.println(urltext);
     if(pagetitle.contains("404")) {
         System.out.println("404 Found");
         System.out.println("FAIL");
         String status="FAIL";

        excelwrite(status,urltext,LastRow);
        }
     else{
       System.out.println("PASS");
       String status = "PASS";
       excelwrite(status,urltext,LastRow);

driver.navigate().back();
     Thread.sleep(4000);
    }

    }
return totallink;
//driver.close();

}
public static String[][] excelRead(String fileName) throws Exception {
File excel = new File(fileName);
FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheet("Sheet1");
int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum];
for (int i = 0 ; i < rowNum ; i++) {
HSSFRow row = ws.getRow(i);
for (int j=0  ; j < colNum ; j++){
HSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
 System.out.println("The value is" + value);

}
}
return data;
} 
public static  String[][] excelwrite(String status,String urltext,int LastRow) throws Exception {
try{
FileInputStream file = new FileInputStream(new File("D:\\Work\\link.xls"));

HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(1);

Row row = sheet.createRow(LastRow);
System.out.println("LastRow value in excelwrite is " + LastRow);

   Cell cell2 = row.createCell(0);
   Cell cell3 = row.createCell(1);
   cell3.setCellValue(status);
   cell2.setCellValue(urltext);
   System.out.println(status);

   file.close();
   FileOutputStream outFile =new FileOutputStream(new File("D:\\Work\\link.xls"));
   workbook.write(outFile);

 }

  catch (FileNotFoundException e) {
   e.printStackTrace();
} 
  catch (IOException e) {
   e.printStackTrace();
}
  catch (HeadlessException e) 
{
e.printStackTrace();
}
return null;
}


public static String cellToString(HSSFCell cell) {
int type;
Object result ;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue();
break;
case 1 :
result = cell.getStringCellValue();
break;
default :
throw new RuntimeException("There are no support for this type of cell");
}
return result.toString();
}
}

============================
OUTPUT:::::::::::

The value isurl
The value ishttps://www.google.co.in/
The value ishttp://www.espire.com/contact-us
The value ishttp://www.travelocity.com/
no of links in 44

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 0 milliseconds

1

1 Answer 1

1

You need to use WebdriverWait for visibility of elements Code: new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.xpath("Xpath"));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.