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

Java org.openqa.selenium.ElementNotVisibleException:元素当前不可见,java,selenium-webdriver,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 ja

下面是我的代码。当我从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
封装测试;
导入java.awt.HeadlessException;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Date;
导入java.util.List;
导入java.util.Properties;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Row;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共类Linktestparam{
公共静态void main(字符串[]args)引发异常{
Properties prop=新属性();
FileInputStream f=新的FileInputStream(“C:\\Documents and Settings\\bibekananda.sarangi\\workspace\\test\\src\\excelPath”);
道具荷载(f);
字符串[][]步;
步骤=excelRead(prop.getProperty(“Linkpath”);
墨水;

对于(int j=1;j您需要使用WebdriverWait查看元素代码: 新建WebDriverWait(driver,30).until(ExpectedConditions.elementtobelickable(By.xpath(“xpath”);