The most common exceptions and their solutions:

NoAlertPresentException

Indicates that a user has tried to access an alert when one is not present.

((JavascriptExecutor) driver).executeScript("alert()");
WebDriverWait wait = new WebDriverWait(driver.getDriver(), 15);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();

More methods to work with alerts here.

NoSuchWindowException

Thrown by WebDriver.switchTo().window(String windowName).
If you get it in IE. Try to specify
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

Or you will have to switch to another window using correct method.

NoSuchElementException

Thrown by WebDriver.findElement(By by) and WebElement.findElement(By by).
Sometimes it happens when your element is located in another iframe. And you'll need to first switch to the iframe element that contains the element that you want to interact with:

driver.switchTo().frame();
eg:

driver.switchTo().frame(1); driver.switchTo().frame(driver.findElement(By.id("id")));
When you've finished interacting with the elements within the frame, you'll need to switch back to the main webpage.
driver.switchTo().defaultContent();

Or you will have to wait for your element will be clickable:

/**
* elementToBeClickable(WebElement element)
*An expectation for checking an element is visible and enabled such that you can click it.
*/
protected boolean validateElementClickable(final By by) {
try{
new WebDriverWait(driver.getDriver(),15).until(ExpectedConditions.elementToBeClickable(by));
}catch(RuntimeException e){
return false;
}
return true;
}

ElementNotVisibleException: element not visible

You will have to wait for your element will be visible:

public boolean validateElementClickable(final By by) {
try{
new WebDriverWait(driver, 15).until(ExpectedConditions.elementToBeClickable(by));
}catch(RuntimeException e){
return false;
}
return true;
}

Stale Element Reference Exception

A stale element reference exception is thrown in one of two cases, the first being more common than the second:

The element has been deleted entirely.
The most frequent cause of this is that page that the element was part of has been refreshed, or the user has navigated away to another page.If the element has been replaced with an identical one, a useful strategy is to look up the element again.

The element is no longer attached to the DOM.it's entirely possible that your code might have a reference to an element that is no longer attached to the DOM. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.

To solve it you can wait for your element become visible:

public boolean validateElementVisible(final By by) {
try{
new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(by));
}catch(RuntimeException e){
return false;
}
return true;
}

SeleniumWatchDog destroyHarder

Instead of driver.quit(); try:
driver.close();
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
Runtime.getRuntime().exec("taskkill /F /IM firefox.exe");
driver.quit();

Close() - It is used to close the browser or page currently which is having the focus.
Quit() - It is used to shut down the web driver instance

If you get exception like “org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"}”

That means that you specified the incorrect url like (without protocol maybe) like:

driver.get("www.google.com");

You will have to change it to:

driver.get("https://google.com");

UnreachableBrowserException

Indicates there was a problem communicating with the browser being controlled or the Selenium server. The most common causes for this exception are:

  • The provided server address to RemoteWebDriver is invalid, so the connection could not be established.
  • The browser has died mid-test.

Check that you have the latest version of webdriver http://www.seleniumhq.org/download/

Check where you use drver.close();

Check if the "close" comes before the @AfterClass is called.

UnhandledAlertException

That means that your test faced unpredictable Alert.

You will have to handle it using special methods below..

Some useful methods for alerts:

protected String getAlertText() {
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.getDriver().switchTo().alert();
// Get the text of the alert or prompt
return alert.getText();
}

public void clickOkInAlert() {
Assert.assertTrue(validateAlertPresent(), "The alert is not present");
// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.getDriver().switchTo().alert();
// Get the text of the alert or prompt
System.out.println("alert: " + alert.getText());
// And acknowledge the alert (equivalent to clicking "OK")
alert.accept();
}

public boolean validateAlertPresent() {
try{
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
}catch(RuntimeException e){
return false;
}
return true;
}

WebDriverException: unknown error: Element is not clickable at point

Try to use instead of driver.findElement(by).click() use:

Actions actions = new Actions(driver);
actions.click(driver.findElement(by)).build().perform();

Timed out receiving message from renderer

Try to use older version of chromedriver (for example instead of using 15th use 12th)

How to handle your webdriver exceptions的更多相关文章

  1. Java+selenium之WebDriver常见特殊情况如iframe/弹窗处理(四)

    1. iframe 的处理 查找元素必须在对应的 ifarme 中查找,否则是找不到的 // 传入参数为 frame 的序号,从0开始 driver.switchTo().frame(Int inde ...

  2. selenium webdriver学习(六)------------如何得到弹出窗口

    selenium webdriver学习(六)------------如何得到弹出窗口 在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id.name的时候.当时还整理 ...

  3. 揭秘Windows10 UWP中的httpclient接口[2]

    阅读目录: 概述 如何选择 System.Net.Http Windows.Web.Http HTTP的常用功能 修改http头部 设置超时 使用身份验证凭据 使用客户端证书 cookie处理 概述 ...

  4. Gulp探究折腾之路(I)

    前言: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优化,而且在开发过程中很多重复的任务能够使用正确的工具自动完成:使用她,我们不仅可以很愉快的编写代码 ...

  5. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...

  6. Selenium 代码收集

    [转载]使用Selenium2测试含有iframe的Ajax网页  原文地址:http://www.cnblogs.com/hexin0614/archive/2012/03/24/2415670.h ...

  7. Gulp探究折腾之路(I)2

    文/晚晴幽草(简书作者)原文链接:http://www.jianshu.com/p/9768a4dc7cf7著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 前言: gulp是前端开发过 ...

  8. 1、揭秘通用平台的 HttpClient (译)

    原文链接:Demystifying HttpClient APIs in the Universal Windows Platform 正打算翻译这篇文章时,发现园子里已经有朋友翻译过了,既然已经开始 ...

  9. Growing Pains for Deep Learning

    Growing Pains for Deep Learning Advances in theory and computer hardware have allowed neural network ...

随机推荐

  1. jQuery Mobile 手动显示ajax加载器

    在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...

  2. 【转】XML的几种读写

    XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境中跨平台的,依赖于内 ...

  3. Linux基础——硬盘分区、格式化及文件系统的管理

    1. 硬件设备与文件名的对应关系 掌握在Linux系统中,每个设备都被当初一个文件来对待. 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a-d] SCSI硬盘 /dev/sd[a-p ...

  4. Django:学习笔记(3)——REST实现

    Django:学习笔记(3)——REST实现 了解REST风格 按照传统的开发方式,我们在实现CURD操作时,会写多个映射路径,比如对一本书的操作,我们会写多个URL,可能如下 web/deleteB ...

  5. React:快速上手(1)——基础知识

    React:快速上手(1)——基础知识 React(有时叫React.js或ReactJS)是一个为数据提供渲染为HTML视图的开源JavaScript库,用于构建用户界面. JSX.元素及渲染 1. ...

  6. linux 查看tomcat 日志

    tomcat 重启: cd /opt/appserver/apache-tomcat-/bin ./shutdown.sh -ef|grep tomcat kill - ./startup.sh 查看 ...

  7. hibernate 一对多、多对多的配置

    一对多 <class name="Question" table="questions" dynamic-insert="true" ...

  8. 配置火星板(MarS Board)的启动参数

    昨天终于拿到了MarS Board.本来上周就应该到的,结果销售人员给我发了块BeagleBone Black... 要是给我的是Sabre Lite也就算了.发错货总是消费者吃亏,好像没怎么听说过占 ...

  9. ubuntu 刚更改默认python3版本后更新包等

    一般来说ubuntu 刚更改为python3为默认版本后要进行一下更新包等等的内容(当然不更新一下也是可以的,最好更新一下,第一次更新较慢) 使用下面两行代码: sudo apt-get update ...

  10. 14.python模块之subprocess

    我们几乎可以在任何操作系统上通过命令行指令与操作系统进行交互,比如Linux平台下的shell.那么我们如何通过Python来完成这些命令行指令的执行呢?另外,我们应该知道的是命令行指令的执行通常有两 ...