2012-12-31 105 views
0

我的腳本每次都沒有成功運行。運行後有時會成功運行1次,下次運行時會失敗。 包示例;我的腳本每次都沒有成功運行。運行後,有時會成功運行1次,下次運行時會失敗

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import jxl.Sheet; 
import jxl.Workbook; 
import jxl.write.Label; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
import java.util.Iterator; 
import java.util.Set; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 

public class Script1 
{ 
public static void main(String args[])throws Exception 
{ 
    //Take the data from input xls file 
    FileInputStream fi =new FileInputStream("D:\\selenium_test\\Object 
     Repositoty\\Input\\Login1.xls"); 
    Workbook wb = Workbook.getWorkbook(fi); 
    Sheet sh = wb.getSheet(0); 

    //Create the Result xls file 
    FileOutputStream fo = new FileOutputStream("D:\\selenium_test\\Object 
    Repositoty\\Output\\Login_Results1.xls"); 
    WritableWorkbook wwb = Workbook.createWorkbook(fo); 
    WritableSheet ws = wwb.createSheet("Results", 0); 
    Thread.sleep(20000); 

    String msg = ""; 

    WebDriver driver = new InternetExplorerDriver(); 
    driver.get("https://www.700creditsolution.com"); 
    driver.manage().window().maximize(); 
    Set<String> windowids = driver.getWindowHandles(); 
    Iterator<String> iter= windowids.iterator(); 
    for (int i = 1; i < sh.getRows(); i++) 
    { 
    while(iter.hasNext()) 
    { 
     System.out.println("Main Window ID : "+iter.next()); 
    } 
    driver.findElement(By.id("lgnLogin_UserName")).clear(); 
    driver.findElement(By.id("lgnLogin_UserName")).sendKeys(sh.getCell(0, 
    i).getContents()); 
    driver.findElement(By.id("lgnLogin_Password")).clear(); 
    driver.findElement(By.id("lgnLogin_Password")).sendKeys(sh.getCell(1, 
    i).getContents()); 
    driver.findElement(By.id("lgnLogin_LoginButton")).click(); 
    Thread.sleep(5000L); 

    windowids = driver.getWindowHandles(); 
    iter= windowids.iterator(); 
    String main_windowID=iter.next(); 
    String tabbed_windowID=iter.next(); 
    System.out.println("Main Window ID :" +main_windowID); 
    Thread.sleep(2000); 
    //switch over to pop-up window 
    driver.switchTo().window(tabbed_windowID); 

    System.out.println("Pop-up window Title : "+driver.getTitle()); 
    driver.findElement(By.xpath("//*[@id='BtnClose']")).click(); 
    Thread.sleep(1000); 
    driver.switchTo().window(main_windowID); 
    msg= driver.findElement(By.xpath("//table[@id='lgnLogin']/tbody/tr/td/table 
    /tbody/tr[4]/td")).getText(); 
    System.out.println(msg); 
    Thread.sleep(2000); 

// driver.get("https://www.700creditsolution.com"); 
// Thread.sleep(2000); 

    for (int j = 0; j < sh.getColumns(); j++) 
    { 
     Label lab=new Label(j,i,sh.getCell(j, i).getContents());  
     ws.addCell(lab); 
     Label result = new Label(2,i, msg); 
     ws.addCell(result); 
     String c2String = sh.getCell(2, i).getContents().toString(); 
     if(c2String.equalsIgnoreCase(msg)) 
     { 
      Label status=new Label(3,i,"Pass"); 
      ws.addCell(status); 
     } 
     else 
     { 
      Label status=new Label(3,i,"Fail"); 
      ws.addCell(status); 
     } 
    } 
    } 

    Label un=new Label(0,0,"User ID"); 
    Label pw=new Label(1,0,"Password"); 
    Label rs=new Label(2,0,"Results"); 
    Label st=new Label(3,0,"Status"); 
    ws.addCell(un); 
    ws.addCell(pw); 
    ws.addCell(rs); 
    ws.addCell(st); 
      wwb.write(); 
      wwb.close(); 

    driver.quit(); 

} 
} 

請儘快幫我解決這個問題。在我的系統中,我有Windows7,IE9。我的應用程序只支持在IE中。幫助將不勝感激。

+0

腳本失敗時的錯誤是什麼? –

+0

錯誤在此位置得到。 。driver.findElement(By.xpath( 「// * [@ id中= 'BtnClose']」))點擊();顯示的錯誤信息是「log4j:WARN無法爲記錄器找到appender(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)。log4j:WARN請正確初始化log4j系統主窗口ID:ac70653a-8834- 4843-bfde-16b229beed98主窗口ID:28deebd4-025e-4eaa-8c66-e59e24e39332主窗口ID:ac70653a-8834-4843-bfde-16b229beed98彈出窗口標題:禁用由於InActivity而禁用用戶警報 –

+0

主窗口ID:8b92c617 -8072-4b25-aa11-47fdc0b54477 彈出窗口標題:700Dealers Inc. 線程「main」中的異常org.openqa.selenium.NoSuchElementException:無法使用xpath查找元素== // * [@ id ='BtnClose ')(警告:服務器未提供任何堆棧跟蹤信息) 命令持續時間或超時:280毫秒 有關此錯誤的文檔,請訪問:http://seleniumhq.or g/exceptions/no_such_element.html 構建信息:版本:'2.21.0',修訂版本:'16552',時間:'2012-04-11 19:08:38'系統信息:os.name:'Windows 7' ,os.arch:'x86',os.version:'6.1',java.version:'1.6.0_23' –

回答

1

您在代碼中沒有處理任何異常。

運行腳本,如果失敗,請嘗試理解異常,爲什麼異常正在生成並正確處理,重複此操作直到您處理所有異常。謝謝。

+0

感謝您的回覆。我嘗試過使用Try&Catch方法來處理異常,但它仍然給我錯誤。 catch(NoSuchElementException e) \t \t { \t \t \t System.out.println(「Element Error:」+ e); \t \t \t e.printStackTrace(); \t \t} \t \t趕上(WebDriverException F) \t \t { \t \t \t的System.out.println( 「webdriver的錯誤:」 + F); \t \t \t f.printStackTrace(); \t \t // \t f.toString(); \t \t} \t趕上(Throwable的任何) \t { 的System.out.println( 「Java錯誤:」 +的話); any.printStackTrace(); } –

+0

捕捉例外是不夠的。你必須妥善處理它們。 –

+0

感謝您的回覆。你能幫我解決如何處理異常情況。因爲我是Java和Webdriver的新手。 –

相關問題