首页 技术 正文
技术 2022年11月19日
0 收藏 329 点赞 4,812 浏览 4738 个字

How to handle your webdriver exceptions

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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,466
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,095
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765