[英]Why doesnt this catch my exception
The below code fails with a Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache... error.
以下代码失败,并在线程“主”中发生异常
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache... error.
Why doesn't it catch my exception in the try block ? 为什么在try块中没有捕获到我的异常?
WebElement listbox = driver.findElement( By.id("ctl00_PlaceHolderMain_SiteDocumentUploadWizard_Wizard1_lsbCountryList"));
Select listboxSelect = new Select(listbox);
//the textbox on the right
WebElement textbox = driver.findElement(By.id("ctl00_PlaceHolderMain_SiteDocumentUploadWizard_Wizard1_lsbCountriesSelected"));
int attempts = 0;
while(textbox.getText() != Contry && attempts < 5)
{
attempts++;
//make your selection in the select list
listboxSelect.selectByVisibleText(Contry);
//click the add button( or use the double click action )
//addCountryButton.click();
action3.perform();
System.out.println("before the try");
//wait for the textbox to be populated
WebDriverWait wait = new WebDriverWait(driver, 10);
try
{
System.out.println("try no "+attempts);
wait.until(ExpectedConditions.textToBePresentInElement(By.id("ctl00_PlaceHolderMain_SiteDocumentUploadWizard_Wizard1_lsbCountriesSelected"), Contry));
}
catch (Exception e){
System.out.println(e.toString());
}catch (Error e){
System.out.println(e.toString());
}
}
Stack trace of exception: 堆栈跟踪异常:
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 1.11 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: 'LCDKHQ087061', ip: '192.168.2.104', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_45'
Session ID: d0861e29-c67b-43ec-beb2-00c4bf29e38e
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=25.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
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:193)
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.getText(RemoteWebElement.java:152)
at mytestpack.JavaExport.DocContrySiteRole(JavaExport.java:250)
at mytestpack.UseInformationArray.UseArray(UseInformationArray.java:24)
at mytestpack.mytestclass.main(mytestclass.java:33)
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.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: 'LCDKHQ087061', ip: '192.168.2.104', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_45'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.cache.getElementAt(resource://fxdriver/modules/web_element_cache.js:7615)
at <anonymous class>.Utils.getElementAt(file:///C:/Users/hlyl/AppData/Local/Temp/anonymous7115004136812534907webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:7233)
at <anonymous class>.WebElement.getElementText(file:///C:/Users/hlyl/AppData/Local/Temp/anonymous7115004136812534907webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10292)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/hlyl/AppData/Local/Temp/anonymous7115004136812534907webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:10844)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/hlyl/AppData/Local/Temp/anonymous7115004136812534907webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js:396)
Just try wrapping the whole above code in try catch
block as it seems Exception is not thrown from surrounded try block containing System.out.println("try no "+attempts);
只是尝试在包装上面的代码整个
try catch
块,因为它似乎异常没有由含有包围try块抛出System.out.println("try no "+attempts);
try
{
WebElement listbox = driver.findElement( By.id("ctl00_PlaceHolderMain_SiteDocumentUploadWizard_Wizard1_lsbCountryList"));
Select listboxSelect = new Select(listbox);
//the textbox on the right
WebElement textbox = driver.findElement(By.id("ctl00_PlaceHolderMain_SiteDocumentUploadWizard_Wizard1_lsbCountriesSelected"));
int attempts = 0;
while(textbox.getText() != Contry && attempts < 5)
{
attempts++;
//make your selection in the select list
listboxSelect.selectByVisibleText(Contry);
//click the add button( or use the double click action )
//addCountryButton.click();
action3.perform();
System.out.println("before the try");
//wait for the textbox to be populated
WebDriverWait wait = new WebDriverWait(driver, 10);
try
{
System.out.println("try no "+attempts);
wait.until(ExpectedConditions.textToBePresentInElement(By.id("ctl00_PlaceHolderMain_SiteDocumentUploadWizard_Wizard1_lsbCountriesSelected"), Contry));
}
catch (Exception e){
System.out.println(e.toString());
}catch (Error e){
System.out.println(e.toString());
}
}
catch (Exception e){
System.out.println(e.toString());
}catch (Error e){
System.out.println(e.toString());
}
} catch (Exception e){
System.out.println(e.toString());
}catch (Error e){
System.out.println(e.toString());
}
一些异常仅继承Throwable
尝试捕获该异常并查看其是否有效。
StaleElementReferenceException
must then be a runtime exception and would have been caught in your try / catch block if it was thrown there. 然后,
StaleElementReferenceException
必须是运行时异常,并且如果抛出在try / catch块中,则会被捕获。
Find line ( JavaExport.java:250
) in your code and ensure you try and catch it there. 在您的代码中找到行(
JavaExport.java:250
),并确保您尝试将其捕获。
As this is selenium you need to make sure that the code is attempting to access exactly what you think it is accessing in the DOM.. 由于这是硒,因此您需要确保代码试图完全访问您认为在DOM中访问的内容。
http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp
More than likely it is the textBox.getText which is throwing the Exception, though this isn't clear without line numbers. textBox.getText很可能引发Exception,尽管没有行号并不清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.