code: public static <T extends WebElement> Function<SearchContext, T> byToFunction(By by) {
          Function<SearchContext, T> function;
          if (by instanceof SingleElementOrNullBy) {
              // will not throw exception, but return null when no element is found
              function = sc -> (T) by.findElement(sc);
          } else {
              function = sc -> {
                  // single element case will throw exception when no element is found
                  List elements = by.findElements(sc);
                  return (elements != null && !elements.isEmpty())? (T) elements.get(0) : null;
              };
          }
          return function;
      }
docs: Converts By to Function returning first result (or null).
  @param by by to convert.
  @return function returning first result of by.