Not able to interact with elements from a webview opened in an IOS native app

Hello.

I’m automating an iOS mobile app.
Real device - iphone 6+, 10.3.3
Appium 1.7.2
Java - compile group: ‘io.appium’, name: ‘java-client’, version: ‘6.0.0-BETA2’
selenium - compile group: ‘org.seleniumhq.selenium’, name: ‘selenium-api’, version: ‘3.8.1’

Everything works ok (native part), until i have 1 section in the app (payment process) where an webview is presented. I can identify elements using safari developer mode:
Ex:

public By getCardType = By.xpath("//*[@id=\"KKName\"]/option[1]");
public By visaCard = By.xpath("//*[@id=\"KKName\"]/option[2]");
public By cardNumberInput = By.xpath("//*[@id=\"cardnumber\"]");
public By cardNumberInput2 = By.id("cardnumber");

When I’m running switch to webview method, it says that no webview is found:

public static void switchToWebview() throws Exception {
    MyLogger.log.info("Trying to switch to first non NATIVE_APP view");
    boolean foundNonNativeContext = false;
    for (String context : getContextHandles()) {
        if ((!foundNonNativeContext) && !NATIVE_APP.equals(context)) {
            foundNonNativeContext = true;
            MyLogger.log.info("Found non native context: " + context);
            switchToContext(context);
        }
    }
    if (!foundNonNativeContext) {
        MyLogger.log.info("ONLY NATIVE_APP CONTEXT AVAILABLE");
    }
}

This are my caps:

caps.setCapability(“platformName”, “iOS”);
caps.setCapability(“platformVersion”, “10.3.3”);
caps.setCapability(“deviceName”, deviceID);
caps.setCapability(“app”, APP_LOCATION());
caps.setCapability(“udid”, DEVICE_ID());
caps.setCapability(“newCommandTimeout”, 600);
caps.setCapability(“automationName”, AUTOMATION_NAME());
// caps.setCapability(MobileCapabilityType.ROTATABLE, true);
caps.setCapability(“wdaConnectionTimeout”, 60000);
caps.setCapability(“resetOnSessionStartOnly”, true);
caps.setCapability(“useNewWDA”, true);
caps.setCapability(“commandTimeouts”, “120000”);
caps.setCapability(“wdaLocalPort”, WDA_PORT());
caps.setCapability(“clearSystemFiles”, true);

What i’m doing wrong?

I’m getting this:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Can someone give me a hint?

Hi @maiky,

We are also having an ios app, in which we have to deal with webview, but without performing any switch, It is working fine.

Try using Below code
public static void switchContext(String context_name)
{
Logs.info(“Before swtich context”+driver.getContext());
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Set contextNames = driver.getContextHandles();
if(!(contextNames.size() >1))
{
Logs.info(“Can’t Switch as only one context is there”);
return;
}
System.out.println(“size of handle”+contextNames.size());
for (String contextName : contextNames) {

        System.out.println("Name of context is"+contextNames);
        driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);

        if (contextName.contains(context_name))
        {
             driver.context(contextName);
             return;
        }

        }

        Logs.info("after switch"+driver.getContext());
}

and Try to recursively call this method at least two times and between each call give implicit wait of 30 seconds

The waiting before switching to webview was the problem. Works perfect with my code sample (also works with your code sample).