2013-12-04 5 views
0

Ниже мой код. Какой-то входной URL из excel в большинстве случаев показывает org.openqa.selenium.ElementNotVisibleException: Элемент в данный момент не отображается. Для сайта, такого как www.travelocity.com, он отображается после нажатия 7 8 ссылок, но для www.google.com он показывает ошибку при запуске.org.openqa.selenium.ElementNotVisibleException: Элемент в настоящее время не виден

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 

Исключение в потоке «основной» org.openqa.selenium.ElementNotVisibleException: Элемент в настоящее время не видно и поэтому не может быть взаимодействовали с длительность команд или тайм-аут: 0 миллисекунды

+0

[Возможный дубликат] (http://stackoverflow.com/questions/12082946/селеном WebDriver-орг-OpenQA-селен-эль ementnotvisibleexception-элемент-это-п) –

ответ

0

Вы должны использовать WebdriverWait для видимости элементов Код: новый WebDriverWait (водитель, 30) .until (ExpectedConditions.elementToBeClickable (By.xpath ("Xpath"));

Смежные вопросы