How to get the text of an element in Appium?

When automating mobile web applications using Appium, extracting text from elements is a crucial task. Whether you need to validate page content, retrieve dynamic data, or perform assertions, getting text reliably is essential.

In this guide, we’ll explore how to get the text of an element using Appium.

We will get the text of the element highlighted below. This element is present at this page – https://testkru.com/Elements/TextMessages

Text element

First, we’ll set the desired capabilities to launch the browser. While we’ll be using the iOS simulator, the same approach also works for the Android simulator.

1
2
3
4
5
6
7
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "iOS");
caps.setCapability("appium:deviceName", "iPhone 16");  // Check simulator name in Xcode
caps.setCapability("appium:automationName", "XCUITest");
caps.setCapability("appium:platformVersion", "18.3");  // Match your simulator's iOS version
caps.setCapability("appium:browserName", "Safari");  // If testing a web app
IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), caps);
  • We can use “id” attribute to locate the element using the findElement() method. The id of our element is “plainText“.
  • We can then use the getText() method to get the element’s text.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class CodekruTest {
 
    public static void main(String[] args) throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("appium:deviceName", "iPhone 16");  // Check simulator name in Xcode
        caps.setCapability("appium:automationName", "XCUITest");
        caps.setCapability("appium:platformVersion", "18.3");  // Match your simulator's iOS version
        caps.setCapability("appium:browserName", "Safari");  // If testing a web app
        IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), caps);
        driver.get("https://testkru.com/Elements/TextMessages");
 
        WebElement element = driver.findElement(By.id("plainText"));  // find element
        System.out.println("Text: " + element.getText());
 
        driver.quit();
    }
 
}

Output –

Text: A Plain Text

The text of an element is also available in the "innerText" and "textContent" properties and we can get their values using the getDomProperty() method provided by Selenium. Here, we’ll use "innerText" to get the visible text. Check out this article to understand the difference between "innerText" and "textContent".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class CodekruTest {
 
    public static void main(String[] args) throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("appium:deviceName", "iPhone 16");  // Check simulator name in Xcode
        caps.setCapability("appium:automationName", "XCUITest");
        caps.setCapability("appium:platformVersion", "18.3");  // Match your simulator's iOS version
        caps.setCapability("appium:browserName", "Safari");  // If testing a web app
        IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), caps);
        driver.get("https://testkru.com/Elements/TextMessages");
 
        WebElement element = driver.findElement(By.id("plainText")); // find element
        System.out.println("Text: " + element.getDomProperty("innerText"));
 
        driver.quit();
    }
 
}

Output –

Text: A Plain Text

We can also use JavascriptExecutor to retrieve the "innerText" property value. This approach is useful when getText() doesn’t work as expected, such as with dynamically rendered elements. JavascriptExecutor allows us to run JavaScript code within Selenium, giving us more control over how we extract text from elements. Here’s how we can do it:

1
2
3
4
WebElement element = driver.findElement(By.id("plainText"));
 
 JavascriptExecutor jse = (JavascriptExecutor) driver;
 System.out.println("Text: " + jse.executeScript("return arguments[0].innerText", element));

Here’s the full code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class CodekruTest {
 
    public static void main(String[] args) throws MalformedURLException{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("appium:deviceName", "iPhone 16");  // Check simulator name in Xcode
        caps.setCapability("appium:automationName", "XCUITest");
        caps.setCapability("appium:platformVersion", "18.3");  // Match your simulator's iOS version
        caps.setCapability("appium:browserName", "Safari");  // If testing a web app
        IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/"), caps);
        driver.get("https://testkru.com/Elements/TextMessages");
 
        WebElement element = driver.findElement(By.id("plainText"));
 
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        System.out.println("Text: " + jse.executeScript("return arguments[0].innerText", element));
 
        driver.quit();
    }
 
}

Output –

Text: A Plain Text

This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *