All Posts

Selenium By and Appium AppiumBy

Selenium By and Appium AppiumBy

As an Appium and Selenium expert, I’ll explain these concepts in a beginner-friendly way.
Both Selenium By and AppiumBy are used to locate elements on an application so that automation scripts can interact with them (click, type, read text, etc.)

Selenium By

In Selenium, By is a class used to locate elements on web applications.
It tells Selenium how to find an element on a web page.

Why Selenium By is needed

  • Web pages contain many elements (buttons, inputs, links, etc.).
  • Selenium needs a clear rule to identify the correct element.
  • By provides different locator strategies to do this.

Common Selenium By locator methods

  • By.id() – Locate by HTML id attribute
  • By.name() – Locate by name attribute
  • By.className() – Locate by CSS class
  • By.tagName() – Locate by HTML tag
  • By.linkText() – Locate link using exact text
  • By.partialLinkText() – Locate link using partial text
  • By.cssSelector() – Locate using CSS selector
  • By.xpath() – Locate using XPath

Selenium By is mainly designed for desktop and mobile web browsers.

Appium AppiumBy

AppiumBy is an Appium-specific locator class used to automate mobile applications
(Android and iOS), including both native apps and mobile web apps.

Why AppiumBy is needed

  • Mobile apps do not use HTML like web apps.
  • Mobile platforms expose elements using platform-specific attributes.
  • AppiumBy supports mobile-only locator strategies.

Common AppiumBy locator methods

  • AppiumBy.id() – Locate by resource-id (Android) or accessibility id
  • AppiumBy.accessibilityId() – Locate using accessibility identifier
  • AppiumBy.className() – Locate by UI class
  • AppiumBy.xpath() – Locate using XPath (works for mobile too)
  • AppiumBy.androidUIAutomator() – Android-specific locator
  • AppiumBy.iOSNsPredicateString() – iOS-specific locator
  • AppiumBy.iOSClassChain() – Faster iOS locator strategy

AppiumBy is optimized for mobile automation and understands
Android and iOS internals.

By and AppiumBy

Aspect Selenium By AppiumBy
Primary Usage Web application automation Mobile application automation
Supported Platforms Desktop and mobile web browsers Android, iOS (native, hybrid, mobile web)
HTML Dependency Works directly with HTML elements Does not rely on HTML (uses mobile UI hierarchy)
Mobile-specific Locators Not supported Supported (UIAutomator, iOS predicates, class chain)
Accessibility ID Support Not available Available and recommended
XPath Support Yes Yes
Performance on Mobile Limited and not optimized Optimized for mobile automation
Package org.openqa.selenium.By io.appium.java_client.AppiumBy

Summary

  • Use Selenium By for automating web applications.
  • Use AppiumBy for automating mobile applications.
  • AppiumBy extends the idea of By to support mobile-specific locators.