org.openqa.selenium.ElementNotVisibleException:元素当前不可见

时间:2013-12-04 11:41:10

标签: java selenium-webdriver

下面是我的代码。我从excel输入url大部分时间显示org.openqa.selenium.ElementNotVisibleException:元素当前不可见错误。 对于像www.travelocity.com这样的网站,它会在点击7 8个链接后显示,但对于www.google.com,它会显示启动时的错误。

    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

线程中的异常&#34; main&#34; org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互     命令持续时间或超时:0毫秒

1 个答案:

答案 0 :(得分:0)

您需要使用WebdriverWait来查看元素代码: new WebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(By.xpath(“Xpath”));