java - org.openqa.selenium.ElementNotVisibleException : Element is not currently visible

标签 java selenium-webdriver

下面是我的代码。当我从 Excel 输入 url 时,大多数时候它会显示 org.openqa.selenium.ElementNotVisibleException: Element is not currentvisible 错误。 对于像 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

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

最佳答案

您需要使用 WebdriverWait 来提高元素的可见性代码: new WebDriverWait(driver, 30).until(ExpectedConditions.elementToBeClickable(By.xpath("Xpath"));

关于java - org.openqa.selenium.ElementNotVisibleException : Element is not currently visible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374409/

相关文章:

python - 无法定位 css_selector 元素 Selenium

java - 向现有 javadoc 添加新类

Java:为什么我的 toString 方法打印错误信息?

node.js - 检查是否找到任一元素

c# - Selenium C# - 将表值与可用值进行比较

java - 在 selenium 中使用 list<webelement> 设置类似的 webblements

java - 从两个 LocalTime 之间的差异获取 LocalTime

java - 如何在 SWT 标签中添加文本和图像

java - 为什么我的文件没有下载 (HTMLUnit) Java

c# - Visual Studio Selenium WebDriver 路径错误