From 506bfb9b216fc127a2f8cf89d5f5ab2f1c894b25 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 16 Nov 2016 10:15:20 +0100 Subject: [PATCH] closeOverlay refactor --- .../com/baeldung/selenium/SeleniumExample.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java index dd309cec79..a9e0ccfb4a 100644 --- a/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java +++ b/selenium-junit-testng/src/main/java/com/baeldung/selenium/SeleniumExample.java @@ -1,14 +1,13 @@ package main.java.com.baeldung.selenium; -import java.util.List; -import java.util.NoSuchElementException; -import java.util.concurrent.TimeUnit; - import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; +import java.util.List; +import java.util.concurrent.TimeUnit; + public class SeleniumExample { private WebDriver webDriver; @@ -38,12 +37,11 @@ public class SeleniumExample { private void closeOverlay() { List webElementList = webDriver.findElements(By.tagName("a")); - try { - if (webElementList != null && !webElementList.isEmpty()) { - webElementList.stream().filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))).findAny().orElseThrow(NoSuchElementException::new).click(); - } - } catch (NoSuchElementException exception) { - exception.printStackTrace(); + if (webElementList != null) { + webElementList.stream() + .filter(webElement -> "Close".equalsIgnoreCase(webElement.getAttribute("title"))) + .findAny() + .ifPresent(WebElement::click); } }