Skip to content

Commit

Permalink
It's expected that an element might not be present...
Browse files Browse the repository at this point in the history
... when waiting for the element to be present.

Closes #3552
  • Loading branch information
shs96c committed Feb 22, 2017
1 parent c624cc0 commit d2b1c51
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,9 @@ public String toString() {
private static WebElement findElement(By by, WebDriver driver) {
try {
return driver.findElements(by).stream().findFirst().orElseThrow(
() -> new NoSuchElementException("Cannot locate an element using " + by));
() -> new NoSuchElementException("Cannot locate an element using " + by));
} catch (NoSuchElementException e) {
throw e;

This comment has been minimized.

Copy link
@mach6

mach6 Feb 22, 2017

Member

@shs96c I'm rusty on lambdas but doesn't this catch and re-throw the NoSuchElemenetException thrown via orElseThrow?

This comment has been minimized.

Copy link
@valfirst

valfirst Feb 22, 2017

Contributor

it does, I would propose another design:

  private static WebElement findElement(By by, WebDriver driver) {
    Optional<WebElement> element;
    try {
      element = driver.findElements(by).stream().findFirst();
    } catch (WebDriverException e) {
      log.log(Level.WARNING,
              String.format("WebDriverException thrown by findElement(%s)", by), e);
      throw e;
    }
    return element.orElseThrow(
            () -> new NoSuchElementException("Cannot locate an element using " + by));
  }
} catch (WebDriverException e) {
log.log(Level.WARNING,
String.format("WebDriverException thrown by findElement(%s)", by), e);
Expand Down

0 comments on commit d2b1c51

Please sign in to comment.