WebDriver(Selenium2) 常见异常及处理方法

Exception NoSuchElementException 
Solutions    
1. Check the locator of your target element. 
2. If the locator is current. Try to wait for page load before find element. 
3. If already wait for long time and always cannot find the element, try to use another type locator. 

Exception  NoSuchWindowException 
Solutions    
1.Check the window’s locator. 
2.Wait page load before find the window. 

Exception  NoAlertPresentException 
Solutions  
1.Make sure the alert( javascript pop window not new window) will present. 
2.Wait page load before deal the alert. 

Exception  NoSuchFrameException 
Solutions  
1.Check the frame’s locator. 
2.Check is the frame has some father frame.(if has father frame you should switch to the father frame first) 
3.Make sure switch to the default content before switch to target frame( only for single frame) 

4.Wait page load before switch to frame. 


Exception    UnhandledAlertException 
Solutions
1. Check if there is some alert dialog present. ( JavaScript pop window). And deal with them. 
2. If no javascript pop window present but the exception still occurs. Make sure the developer tools is closed when running automation case. (Because since selenium 2.19. “UnhandledAlertException” added and they think the developer tool is an alert) 

Exception  UnexpectedTagNameException 
Solutions  
1.Check the target element’s html tag name. 
2.Try to wait for page load then initializing the selector. 

Exception  StaleElementReferenceException 
Solutions  
1.Re-find the element again. (Because the element has been refresh.) 

Exception  TimeoutException 
Solutions  
1. Check the expected conditions locator. 

2..Increase the wait time. 


以上来源:http://uniquepig.iteye.com/blog/1568197

***********************************************************************************************************

另一篇解决元素过期的文章如下:

用Selenium WebDriver获取WebElement时的元素过期问题

  (2012-04-24 11:12:14)
例如我们有一排PageLink:
用Selenium <wbr>WebDriver获取WebElement时的元素过期问题

对应的html为:

<a class="PageLink" title="2" href="/shanghai?pageno=2">2</a>
<a class="PageLink" title="3" href="/shanghai?pageno=3">3</a>
<a class="PageLink" title="4" href="/shanghai?pageno=4">4</a>
<a class="PageLink" title="5" href="/shanghai?pageno=5">5</a>
<a class="PageLink" title="6" href="/shanghai?pageno=6">6</a>
<a class="PageLink" title="7" href="/shanghai?pageno=7">7</a>
<a class="PageLink" title="8" href="/shanghai?pageno=8">8</a>
<a class="PageLink" title="9" href="/shanghai?pageno=9">9</a>
<a class="PageLink" title="10" href="/shanghai?pageno=10">10</a>

原本在构造函数中通过driver.findElements(By.xpath("//a[@class = 'PageLink')来存储所有的PageLink到一个List<WebElement>中,然后通过Iterator.Next()的方式遍历所有PageLink页。

但在第三页就出现异常:提示
org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

因此可以看出,在经过一次点击后,原有PageLink已经失效。需要重新获取。
其原因在于,点击过一次PageLink后,会重新刷新并生成新PageLink,当前页的PageLink不会显示。
用Selenium <wbr>WebDriver获取WebElement时的元素过期问题


因此解决方法是设定了两个参数
private Integer currentPageLinkNumber = 1;
private Integer MaxPage = 10;//Max page links number

然后通过

while(currentPageLinkNumber<MaxPage)
{
WebElement PageLink;
PageLink = driver.findElement(By.xpath("//a[@class = 'PageLink' and @title ='"+Integer.toString(currentPageLinkNumber+1)+"']"));
PageLink.click();
currentPageLinkNumber++;

//OtherOperation();
}

的方式进行迭代。

虽然感觉很麻烦就是- -
要很小心的注意同步currentPageLinkNumber和当前的PageLink啊- -

你可能感兴趣的:(WebDriver(Selenium2) 常见异常及处理方法)