Appium Can't Locate Element in Webview in Native App After Switching Contexts

Hey! So I’m a relatively new Appium user, so please excuse me if this question seems a little basic.

My company has a hybrid application which uses webiews in several places, and I am currently in the middle of writing test scripts for this application in Ruby using Appium. I’ve been stuck for a while, because I reach a page where I need to search for elements inside a webview and click on them. So far, I’ve managed to switch contexts to the webview using

set_context context_array.last

and I know that the context switch is successful, because I then output my current context to the terminal, and it correctly outputs that I’m in the webview. I then used Safari to inspect the elements in my webview, and saw the following HTML code:

I followed the xlink and found the following lines of code:
`

icon-eat`

So in my Appium script, I’ve tried to locate the element using the following, which so far haven’t worked.

  1. wait { xpath('//symbol[@id="icon-eat"]') }
  2. wait { id("icon-eat") }

Any advice on how to proceed? My capabilities are:
options = { caps: { platformName: 'iOS', platformVersion: '10.3', deviceName: 'iPhone 7', app: APP_PATH, automationName: "XCUITest", browserName: "iOS", startIWDP: true, nativeWebTap: true }, launchTimeout: 20000 }

An Appium log is attached. I’ve replaced the company name and other sensitive details for security purposes.
Appium Logs.txt (54.8 KB)

I just dealt with the same issue. After switching from the Native App content to the Webview context, call driver.getPageSource(), then search for the element. Below is some pseudocode to outline the method described:

switchToWebContext();
driver.getPageSource();
driver.findElement(YOUR_WEB_ELEMENT);

Also, if you can, use the cssSelector instead of xpath. I found that the results were more reliable, and the element is located more quickly.