读取和写入Excel文件时出现意外错误使用Selenium Webdriver

时间:2013-10-29 13:05:22

标签: excel junit selenium-webdriver

我正在使用此方法阅读

FileInputStream fi = new FileInputStream("C:\\Users\\GChaitanya.CHAINGANG\\Desktop\\2012_October_3PL_Unified_Template.xls");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(0);

这就是写

WritableWorkbook wb = Workbook.createWorkbook(new File("D:\\output_2.xls"));
WritableSheet ws = wb.createSheet("customsheet", 1);

我收到此错误

org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 100.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.34.0', revision: '11cd0ef', time: '2013-08-06 17:11:28'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'
Session ID: 60c039f3-60bb-413f-aca5-0543e074b04e
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=24.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79)
    at gmailTest.TestLink.RealTime(TestLink.java:100)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element not found in the cache - perhaps the page has changed since it was looked up
Build info: version: '2.34.0', revision: '11cd0ef', time: '2013-08-06 17:11:28'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45'
Driver info: driver.version: unknown
    at <anonymous class>.fxdriver.cache.getElementAt(resource://fxdriver/modules/web_element_cache.js:7642)
    at <anonymous class>.Utils.getElementAt(file:///C:/Users/GCHAIT~1.CHA/AppData/Local/Temp/anonymous5288729173687770784webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:7231)
    at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/GCHAIT~1.CHA/AppData/Local/Temp/anonymous5288729173687770784webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:8228)
    at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/GCHAIT~1.CHA/AppData/Local/Temp/anonymous5288729173687770784webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10799)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/GCHAIT~1.CHA/AppData/Local/Temp/anonymous5288729173687770784webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10816)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/GCHAIT~1.CHA/AppData/Local/Temp/anonymous5288729173687770784webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:396)

并在调试模式下显示

<terminated, exit value: 0>C:\Program Files\Java\jre7\bin\javaw.exe (Oct 29, 2013 5:12:25 PM)   

代码工作正常,但突然停止工作,几乎没有读取10条记录和崩溃。我能够阅读至少300到400条记录

2 个答案:

答案 0 :(得分:0)

package realDatatesting;
import java.io.FileInputStream;
import java.util.concurrent.TimeUnit;
//import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
//import org.openqa.selenium.remote.RemoteWebDriver;
//import com.google.common.collect.Table.Cell;
public class Realtime_snap{
Sheet s;
WebDriver driver;
@Before
public void setUp()
{
 driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
}
/**
* @throws Exception
*/
@Test
public void RealTime() throws Exception
{

driver.get("----URl---");

//Passing the user name and password
driver.findElement(By.id("-----")).sendKeys("-----");
driver.findElement(By.id("-------")).sendKeys("------");
//Clicking on login button
driver.findElement(By.id("-------")).click();

//Selecting real time estimator
driver.findElement(By.xpath(".//*[@id='nav']/li[3]/a")).click();
driver.findElement(By.xpath(".//*[@id='nav']/li[3]/ul/li[2]/a")).click();


//Selecting model
driver.findElement(By.xpath("----")).click();
driver.findElement(By.xpath("------")).click();

FileInputStream fi = new FileInputStream("---Filepath");
Workbook w = Workbook.getWorkbook(fi);
s = w.getSheet(0);
int totalrows=s.getRows();
for(int row=31; row < totalrows;row++)
{
    Thread.sleep(1000);
    s = w.getSheet(0);
    WritableWorkbook wb = Workbook.createWorkbook(new File("D:\\output1.xls"));
    WritableSheet ws = wb.createSheet("customsheet", 1);


    Thread.sleep(1000);
    String origin = s.getCell(1,row).getContents();
    driver.findElement(By.xpath("----")).sendKeys(origin);

    .
            .
            .
    String TempCont = s.getCell(13,row).getContents();
    driver.findElement(By.xpath("------")).sendKeys(TempCont);

    //Click's on calculate button
    driver.findElement(By.xpath("------")).click();
    Thread.sleep(1000);



        String m1 = (driver.findElement(By.id("------")).getText());
        Label label1 = new Label(1, row, m1);
        ws.addCell(label1);

      .
      .
      .

        String m17 = (driver.findElement(By.id("ctl00_ContentPlaceHolderBody_ucModelDataEntry1_lblRegionalVariability")).getText());
        Label Label17 = new Label(18, row, m17);
        ws.addCell(Label17);
      //Click on Reset
    driver.findElement(By.xpath(".//*[@id='ctl00_ContentPlaceHolderBody_ucModelDataEntry1_resetBtn']")).click();
    Thread.sleep(1000);

    //Clicking on logout

     wb.write();                                                            

        wb.close();

}

} 



@After
public void tearDown() throws InterruptedException
{
Thread.sleep(10000);        
driver.findElement(By.xpath(".-----")).click();
driver.close();
driver.quit();
}
}

这整个代码工作正常我不明白出了什么问题尝试了很多方法,但都没有用。

答案 1 :(得分:0)

这不是与读取或写入Excel文件相关的错误。您在以下行使用Selenium单击Web元素时收到错误(StaleElementReferenceException)

在gmailTest.TestLink.RealTime(TestLink.java:100)

在点击webelement之前,请确保该元素具有有效的最新引用。

查看StaleElementReferenceException http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp