Repository: appium/java-client
Issue: Client asking to many times for session

## Description

I'm seeing the java client asking for getSession too many times, probably like 5,6 times between commands, making the test to run slower. This make a big difference when running on a testing cloud since this calls are adding ~4 seconds between commands in the test. 

## Environment
This is a problem with the client itself. 
* java client build version or git revision if you use some shapshot: 5.0.4 (havent tried another version)
* Appium server version or git revision if you use some shapshot: Any
* Desktop OS/version used to run Appium if necessary: Any
* Node.js version (unless using Appium.app|exe) or Appium CLI or Appium.app|exe: Any
* Mobile platform/version under test: Any
* Real device or emulator/simulator: Any

## Details




## Code To Reproduce Issue [ Good To Have ]

Running a simple test using the java client, during logs we can see all the getSession commands. 
For ex.
```
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, findElements {using=id, value=com.test.app:id/text1}]
Nov 09, 2017 5:39:45 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:45 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:45 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:46 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:47 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:47 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:47 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:47 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:47 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:47 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:48 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:49 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:49 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:49 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:49 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:49 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:49 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getSession [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:50 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getSession {}]
Nov 09, 2017 5:39:50 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: getElementText [f8b4312dda7c43bfba2620ef50605eec, getElementText {id=5201}]
Nov 09, 2017 5:39:50 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [f8b4312dda7c43bfba2620ef50605eec, getElementText {id=5201}]
```

## Ecxeption stacktraces

No errors. 

## Link to Appium logs


Changed files:
src/main/java/io/appium/java_client/internal/ElementMap.java
src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java
src/main/java/io/appium/java_client/pagefactory/utils/WebDriverUnpackUtility.java
src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java
src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java
src/main/java/io/appium/java_client/pagefactory/WidgetListInterceptor.java
src/main/java/io/appium/java_client/pagefactory/bys/ContentMappedBy.java
src/main/java/io/appium/java_client/DefaultGenericMobileDriver.java

diff --git a/src/main/java/io/appium/java_client/DefaultGenericMobileDriver.java b/src/main/java/io/appium/java_client/DefaultGenericMobileDriver.java
index 23e188b2..6376a265 100644
--- a/src/main/java/io/appium/java_client/DefaultGenericMobileDriver.java
+++ b/src/main/java/io/appium/java_client/DefaultGenericMobileDriver.java
@@ -153,7 +153,7 @@ abstract class DefaultGenericMobileDriver<T extends WebElement> extends RemoteWe
 
     @Override
     public String toString() {
-        return String.format("%s: %s", getPlatformName(),
-                getAutomationName());
+        return String.format("%s, Capabilities: %s", getClass().getCanonicalName(),
+                getCapabilities().asMap().toString());
     }
 }
diff --git a/src/main/java/io/appium/java_client/internal/ElementMap.java b/src/main/java/io/appium/java_client/internal/ElementMap.java
index 1d150ae9..65ff376e 100644
--- a/src/main/java/io/appium/java_client/internal/ElementMap.java
+++ b/src/main/java/io/appium/java_client/internal/ElementMap.java
@@ -16,9 +16,10 @@
 
 package io.appium.java_client.internal;
 
+import static org.apache.commons.lang3.StringUtils.isBlank;
+
 import com.google.common.collect.ImmutableMap;
 
-import io.appium.java_client.HasSessionDetails;
 import io.appium.java_client.MobileElement;
 import io.appium.java_client.android.AndroidElement;
 import io.appium.java_client.ios.IOSElement;
@@ -68,17 +69,19 @@ public enum ElementMap {
     }
 
     /**
-     * @param hasSessionDetails something that implements {@link io.appium.java_client.HasSessionDetails}.
-     * @return subclass of {@link io.appium.java_client.MobileElement} that convenient to current session details.
+     * @param platform is the mobile platform. See {@link MobilePlatform}.
+     * @param automation is the mobile automation type. See {@link AutomationName}
+     * @return subclass of {@link org.openqa.selenium.remote.RemoteWebElement} that convenient
+     *     to current session details.
      */
-    public static Class<? extends RemoteWebElement> getElementClass(HasSessionDetails hasSessionDetails) {
-        if (hasSessionDetails == null) {
+    public static Class<? extends RemoteWebElement> getElementClass(String platform, String automation) {
+        if (isBlank(platform) && isBlank(automation)) {
             return RemoteWebElement.class;
         }
-        ElementMap element = Optional.ofNullable(mobileElementMap.get(String
-                .valueOf(hasSessionDetails.getAutomationName()).toLowerCase().trim()))
+        ElementMap element = Optional.ofNullable(mobileElementMap.get(
+                String.valueOf(platform).toLowerCase().trim()))
                 .orElseGet(() -> mobileElementMap
-                        .get(String.valueOf(hasSessionDetails.getPlatformName()).toLowerCase().trim()));
+                        .get(String.valueOf(automation).toLowerCase().trim()));
         if (element == null) {
             return RemoteWebElement.class;
         }
diff --git a/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java b/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java
index 923910e7..5d752f64 100644
--- a/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java
+++ b/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java
@@ -37,7 +37,10 @@ import java.util.Collection;
 public class JsonToMobileElementConverter extends JsonToWebElementConverter {
 
     protected final RemoteWebDriver driver;
-    private final HasSessionDetails hasSessionDetails;
+
+    private final String platform;
+    private final String automation;
+
 
     /**
      * @param driver an instance of {@link org.openqa.selenium.remote.RemoteWebDriver} subclass
@@ -46,7 +49,8 @@ public class JsonToMobileElementConverter extends JsonToWebElementConverter {
     public JsonToMobileElementConverter(RemoteWebDriver driver, HasSessionDetails hasSessionDetails) {
         super(driver);
         this.driver = driver;
-        this.hasSessionDetails = hasSessionDetails;
+        this.platform = hasSessionDetails.getPlatformName();
+        this.automation = hasSessionDetails.getAutomationName();
     }
 
     /**
@@ -80,9 +84,9 @@ public class JsonToMobileElementConverter extends JsonToWebElementConverter {
         return result;
     }
 
-    protected RemoteWebElement newMobileElement() {
+    private RemoteWebElement newMobileElement() {
         Class<? extends RemoteWebElement> target;
-        target = getElementClass(hasSessionDetails);
+        target = getElementClass(platform, automation);
         try {
             Constructor<? extends RemoteWebElement> constructor = target.getDeclaredConstructor();
             constructor.setAccessible(true);
diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java
index a08ffc31..a7acc71a 100644
--- a/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java
+++ b/src/main/java/io/appium/java_client/pagefactory/AppiumElementLocator.java
@@ -19,8 +19,11 @@ package io.appium.java_client.pagefactory;
 import static io.appium.java_client.pagefactory.ThrowableUtil.extractReadableException;
 import static io.appium.java_client.pagefactory.ThrowableUtil.isInvalidSelectorRootCause;
 import static io.appium.java_client.pagefactory.ThrowableUtil.isStaleElementReferenceException;
+import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.getCurrentContentType;
+import static java.lang.String.format;
 
 
+import io.appium.java_client.pagefactory.bys.ContentMappedBy;
 import io.appium.java_client.pagefactory.locator.CacheableLocator;
 
 import org.openqa.selenium.By;
@@ -39,13 +42,15 @@ import java.util.function.Supplier;
 
 class AppiumElementLocator implements CacheableLocator {
 
+    private static final String exceptionMessageIfElementNotFound = "Can't locate an element by this strategy: %s";
+
     private final boolean shouldCache;
     private final By by;
     private final TimeOutDuration duration;
     private final SearchContext searchContext;
     private WebElement cachedElement;
     private List<WebElement> cachedElementList;
-    private final String exceptionMessageIfElementNotFound;
+
     /**
      * Creates a new mobile element locator. It instantiates {@link WebElement}
      * using @AndroidFindBy (-s), @iOSFindBy (-s) and @FindBy (-s) annotation
@@ -64,7 +69,25 @@ class AppiumElementLocator implements CacheableLocator {
         this.shouldCache = shouldCache;
         this.duration = duration;
         this.by = by;
-        this.exceptionMessageIfElementNotFound =  "Can't locate an element by this strategy: " + by.toString();
+    }
+
+    /**
+     * This methods makes sets some settings of the {@link By} according to
+     * the given instance of {@link SearchContext}. If there is some {@link ContentMappedBy}
+     * then it is switched to the searching for some html or native mobile element.
+     * Otherwise nothing happens there.
+     *
+     * @param currentBy is some locator strategy
+     * @param currentContent is an instance of some subclass of the {@link SearchContext}.
+     * @return the corrected {@link By} for the further searching
+     */
+    private static By getBy(By currentBy, SearchContext currentContent) {
+        if (!ContentMappedBy.class.isAssignableFrom(currentBy.getClass())) {
+            return currentBy;
+        }
+
+        return ContentMappedBy.class.cast(currentBy)
+                .useContent(getCurrentContentType(currentContent));
     }
 
     private <T> T waitFor(Supplier<T> supplier) {
@@ -91,15 +114,16 @@ class AppiumElementLocator implements CacheableLocator {
             return cachedElement;
         }
 
+        By bySearching = getBy(this.by, searchContext);
         try {
             WebElement result =  waitFor(() ->
-                    searchContext.findElement(by));
+                    searchContext.findElement(bySearching));
             if (shouldCache) {
                 cachedElement = result;
             }
             return result;
         } catch (TimeoutException | StaleElementReferenceException e) {
-            throw new NoSuchElementException(exceptionMessageIfElementNotFound, e);
+            throw new NoSuchElementException(format(exceptionMessageIfElementNotFound, bySearching.toString()), e);
         }
     }
 
@@ -114,11 +138,9 @@ class AppiumElementLocator implements CacheableLocator {
         List<WebElement> result;
         try {
             result = waitFor(() -> {
-                List<WebElement> list = searchContext.findElements(by);
-                if (list.size() > 0) {
-                    return list;
-                }
-                return null;
+                List<WebElement> list = searchContext
+                        .findElements(getBy(by, searchContext));
+                return list.size() > 0 ? list : null;
             });
         } catch (TimeoutException | StaleElementReferenceException e) {
             result = new ArrayList<>();
@@ -135,7 +157,7 @@ class AppiumElementLocator implements CacheableLocator {
     }
 
     @Override public String toString() {
-        return String.format("Located by %s", by);
+        return format("Located by %s", by);
     }
 
 
diff --git a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java
index 9753736e..9d189d31 100644
--- a/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java
+++ b/src/main/java/io/appium/java_client/pagefactory/AppiumFieldDecorator.java
@@ -20,6 +20,7 @@ import static io.appium.java_client.internal.ElementMap.getElementClass;
 import static io.appium.java_client.pagefactory.utils.ProxyFactory.getEnhancedProxy;
 import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility
     .unpackWebDriverFromSearchContext;
+import static java.util.Optional.ofNullable;
 
 import com.google.common.collect.ImmutableList;
 
@@ -64,13 +65,12 @@ public class AppiumFieldDecorator implements FieldDecorator {
             IOSElement.class, WindowsElement.class);
     public static long DEFAULT_TIMEOUT = 1;
     public static TimeUnit DEFAULT_TIMEUNIT = TimeUnit.SECONDS;
-    private final WebDriver originalDriver;
+    private final WebDriver webDriver;
     private final DefaultFieldDecorator defaultElementFieldDecoracor;
     private final AppiumElementLocatorFactory widgetLocatorFactory;
     private final String platform;
     private final String automation;
     private final TimeOutDuration duration;
-    private final HasSessionDetails hasSessionDetails;
 
 
     public AppiumFieldDecorator(SearchContext context, long timeout,
@@ -87,14 +87,18 @@ public class AppiumFieldDecorator implements FieldDecorator {
      * @param duration is a desired duration of the waiting for an element presence.
      */
     public AppiumFieldDecorator(SearchContext context, TimeOutDuration duration) {
-        this.originalDriver = unpackWebDriverFromSearchContext(context);
-        if (originalDriver == null
-                || !HasSessionDetails.class.isAssignableFrom(originalDriver.getClass())) {
-            hasSessionDetails = null;
+        this.webDriver = unpackWebDriverFromSearchContext(context);
+        HasSessionDetails hasSessionDetails = ofNullable(this.webDriver).map(webDriver -> {
+            if (!HasSessionDetails.class.isAssignableFrom(webDriver.getClass())) {
+                return null;
+            }
+            return HasSessionDetails.class.cast(webDriver);
+        }).orElse(null);
+
+        if (hasSessionDetails == null) {
             platform = null;
             automation = null;
         } else {
-            hasSessionDetails = HasSessionDetails.class.cast(originalDriver);
             platform = hasSessionDetails.getPlatformName();
             automation = hasSessionDetails.getAutomationName();
         }
@@ -202,7 +206,7 @@ public class AppiumFieldDecorator implements FieldDecorator {
 
         if (isAlist) {
             return getEnhancedProxy(ArrayList.class,
-                new WidgetListInterceptor(locator, originalDriver, map, widgetType,
+                new WidgetListInterceptor(locator, webDriver, map, widgetType,
                         duration));
         }
 
@@ -210,11 +214,11 @@ public class AppiumFieldDecorator implements FieldDecorator {
             WidgetConstructorUtil.findConvenientConstructor(widgetType);
         return getEnhancedProxy(widgetType, new Class[] {constructor.getParameterTypes()[0]},
             new Object[] {proxyForAnElement(locator)},
-            new WidgetInterceptor(locator, originalDriver, null, map, duration));
+            new WidgetInterceptor(locator, webDriver, null, map, duration));
     }
 
     private WebElement proxyForAnElement(ElementLocator locator) {
-        ElementInterceptor elementInterceptor = new ElementInterceptor(locator, originalDriver);
-        return getEnhancedProxy(getElementClass(hasSessionDetails), elementInterceptor);
+        ElementInterceptor elementInterceptor = new ElementInterceptor(locator, webDriver);
+        return getEnhancedProxy(getElementClass(platform, automation), elementInterceptor);
     }
 }
diff --git a/src/main/java/io/appium/java_client/pagefactory/WidgetListInterceptor.java b/src/main/java/io/appium/java_client/pagefactory/WidgetListInterceptor.java
index ff4a9f74..9c752322 100644
--- a/src/main/java/io/appium/java_client/pagefactory/WidgetListInterceptor.java
+++ b/src/main/java/io/appium/java_client/pagefactory/WidgetListInterceptor.java
@@ -18,6 +18,7 @@ package io.appium.java_client.pagefactory;
 
 import static io.appium.java_client.pagefactory.ThrowableUtil.extractReadableException;
 import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.getCurrentContentType;
+import static java.util.Optional.ofNullable;
 
 import io.appium.java_client.pagefactory.bys.ContentType;
 import io.appium.java_client.pagefactory.interceptors.InterceptorOfAListOfElements;
@@ -59,8 +60,9 @@ class WidgetListInterceptor extends InterceptorOfAListOfElements {
             cachedElements = elements;
             cachedWidgets.clear();
 
+            ContentType type = null;
             for (WebElement element : cachedElements) {
-                ContentType type = getCurrentContentType(element);
+                type = ofNullable(type).orElseGet(() -> getCurrentContentType(element));
                 Class<?>[] params =
                     new Class<?>[] {instantiationMap.get(type).getParameterTypes()[0]};
                 cachedWidgets.add(ProxyFactory
diff --git a/src/main/java/io/appium/java_client/pagefactory/bys/ContentMappedBy.java b/src/main/java/io/appium/java_client/pagefactory/bys/ContentMappedBy.java
index 95a6659c..1f995bbe 100644
--- a/src/main/java/io/appium/java_client/pagefactory/bys/ContentMappedBy.java
+++ b/src/main/java/io/appium/java_client/pagefactory/bys/ContentMappedBy.java
@@ -16,7 +16,8 @@
 
 package io.appium.java_client.pagefactory.bys;
 
-import static io.appium.java_client.pagefactory.utils.WebDriverUnpackUtility.getCurrentContentType;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static io.appium.java_client.pagefactory.bys.ContentType.NATIVE_MOBILE_SPECIFIC;
 
 import org.openqa.selenium.By;
 import org.openqa.selenium.SearchContext;
@@ -24,32 +25,36 @@ import org.openqa.selenium.WebElement;
 
 import java.util.List;
 import java.util.Map;
+import javax.annotation.Nonnull;
 
 public class ContentMappedBy extends By {
     private final Map<ContentType, By> map;
+    private ContentType currentContent = NATIVE_MOBILE_SPECIFIC;
 
     public ContentMappedBy(Map<ContentType, By> map) {
         this.map = map;
     }
 
+    /**
+     * This method sets required content type for the further searching.
+     * @param type required content type {@link ContentType}
+     * @return self-reference.
+     */
+    public By useContent(@Nonnull ContentType type) {
+        checkNotNull(type);
+        currentContent = type;
+        return this;
+    }
+
     @Override public WebElement findElement(SearchContext context) {
-        return context.findElement(map.get(getCurrentContentType(context)));
+        return context.findElement(map.get(currentContent));
     }
 
     @Override public List<WebElement> findElements(SearchContext context) {
-        return context.findElements(map.get(getCurrentContentType(context)));
+        return context.findElements(map.get(currentContent));
     }
 
     @Override public String toString() {
-        By defaultBy = map.get(ContentType.HTML_OR_DEFAULT);
-        By nativeBy = map.get(ContentType.NATIVE_MOBILE_SPECIFIC);
-
-        if (defaultBy.equals(nativeBy)) {
-            return defaultBy.toString();
-        }
-
-        return "Locator map: " + "\\n"
-            + "- native content: \\"" + nativeBy.toString() + "\\" \\n"
-            + "- html content: \\"" + defaultBy.toString() + "\\"";
+        return map.get(currentContent).toString();
     }
 }
diff --git a/src/main/java/io/appium/java_client/pagefactory/utils/WebDriverUnpackUtility.java b/src/main/java/io/appium/java_client/pagefactory/utils/WebDriverUnpackUtility.java
index 279ba4f0..ac004a41 100644
--- a/src/main/java/io/appium/java_client/pagefactory/utils/WebDriverUnpackUtility.java
+++ b/src/main/java/io/appium/java_client/pagefactory/utils/WebDriverUnpackUtility.java
@@ -19,6 +19,7 @@ package io.appium.java_client.pagefactory.utils;
 import static io.appium.java_client.pagefactory.bys.ContentType.HTML_OR_DEFAULT;
 import static io.appium.java_client.pagefactory.bys.ContentType.NATIVE_MOBILE_SPECIFIC;
 import static java.util.Optional.ofNullable;
+import static org.apache.commons.lang3.StringUtils.containsIgnoreCase;
 
 import io.appium.java_client.HasSessionDetails;
 import io.appium.java_client.pagefactory.bys.ContentType;
@@ -77,7 +78,8 @@ public final class WebDriverUnpackUtility {
      *                extension/implementation.
      *                Note: if you want to use your own implementation then it should
      *                implement {@link org.openqa.selenium.ContextAware} or
-     *                {@link org.openqa.selenium.internal.WrapsDriver}
+     *                {@link org.openqa.selenium.internal.WrapsDriver} or
+     *                {@link HasSessionDetails}
      * @return current content type. It depends on current context. If current context is
      *     NATIVE_APP it will return
      * {@link io.appium.java_client.pagefactory.bys.ContentType#NATIVE_MOBILE_SPECIFIC}.
@@ -93,20 +95,17 @@ public final class WebDriverUnpackUtility {
             if (HasSessionDetails.class.isAssignableFrom(driver.getClass())) {
                 HasSessionDetails hasSessionDetails = HasSessionDetails.class.cast(driver);
 
-                if (hasSessionDetails.isBrowser()) {
-                    return HTML_OR_DEFAULT;
+                if (!hasSessionDetails.isBrowser()) {
+                    return NATIVE_MOBILE_SPECIFIC;
                 }
-                return NATIVE_MOBILE_SPECIFIC;
-            }
-
-            if (!ContextAware.class.isAssignableFrom(driver.getClass())) { //it is desktop browser
-                return HTML_OR_DEFAULT;
             }
 
-            ContextAware contextAware = ContextAware.class.cast(driver);
-            String currentContext = contextAware.getContext();
-            if (currentContext.contains(NATIVE_APP_PATTERN)) {
-                return NATIVE_MOBILE_SPECIFIC;
+            if (ContextAware.class.isAssignableFrom(driver.getClass())) { //it is desktop browser
+                ContextAware contextAware = ContextAware.class.cast(driver);
+                String currentContext = contextAware.getContext();
+                if (containsIgnoreCase(currentContext, NATIVE_APP_PATTERN)) {
+                    return NATIVE_MOBILE_SPECIFIC;
+                }
             }
 
             return HTML_OR_DEFAULT;