Is there a way to get the text from an iOS element after accessing it By.xpath?

I am using findElement(By.xpath()) to access all elements, and would like to continue doing so. However, this returns null:

WebElement e = driver.findElement(By.xpath(“my xpath”));

And this throws an exception:

I tried both after casting the element to MobileElement, and got the same result

There’s a few different text-related attributes on an WebElement in iOS.

In Python, below:

WebElement el = driver.findElement(By.xpath("my xpath"))

print 'TESTMODE: ' + el.text, el.get_attribute("name"), el.get_attribute("label")

The Java twin for get_attribute is: getAttribute

You might want to try a different attribute such as name. If you look at the page source, it’ll show the available options.

Thanks! But what do you mean by “look at the page source”?

Page Source is the view hierarchy of the application. Similar to the DOM in a webpage.
It has attributes of each web element, xpath, name, text defined in it.

You should be able to do or element.text depending on which one gives you the information you need to identify the element.

Python: driver.page_source

I think Java is driver.getPageSource() (source)

Thanks again! The pageSource call will be very helpful.

It turns out that I was mistaken about getText() not working! The field from which I was trying to get the text had no text! There is content visible to the naked eye, but pageSource() does not show it. Can’t figure that out, but that’s another issue.

For these situations, you’d want to set an accessibility identifier.

i did set accessibility identifier but facing issue