Switch to WebView using getcontexthandles()

Yes, i am using driver.getcontextHandles() but got Error under getcontextHandles(), it says undefined method for RemoteWebdriver

Try using AppiumDriver

when i am using AppiumDriver , then it shows Error for Appium Driver

Weird. I have the below import statement
import io.appium.java_client.AppiumDriver;

I was tried it also… it shows error for " io "…

How are you running your testcase. How is your project set up

I am simply Set Build Path suing Selenium jar files and and Using TestNG Framework.

I set capability using RemoteWebDriver.

Please Share me the detail what needs to import for Appium Driver? so that i can use getcontexthandles()

I use Maven project which comes with all jar files.
Looks like AppiumDriver is in java-client-1.3.0.jar

No, I am not using it… let me download it … after downloading it… should i set path in Eclipse ?

Yes you can find lots of turorial in internet

Sample code for switch to WebView & Native using getcontexthandles using java

driver = new AppiumDriver(new URL(“”), capabilities);
Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
driver.context(contextNames.toArray()[1]); // set context to WEBVIEW_1
String myText = driver.findElement(By.cssSelector(".green_button")).click();
Test Automation

1 Like

My code also not switching to Webview ,

System.out.println(((AppiumDriver) driver).getContextHandles());
		for (String contextName : ((AppiumDriver) driver).getContextHandles()) {
		    if (contextName.contains("WEBVIEW")){
		    	((AppiumDriver) driver).context(contextName);
		    	System.out.println("Switched to " + contextName);

It is showing only NATIVE_APP. But if I am executing with Selendroid it was working fine with the code

for(String winHandle : driver.getWindowHandles()){
				if (winHandle.contains("WEBVIEW"))

Please help me on this

1 Like

Hi @Fayizkc,

May be using a cast of AndroidDriver might help you.


Not working
It is working fine only with selendroid, But not working even if I am using AndroidDriver.

 info: --> GET /wd/hub/session/9e911421-eab2-45e7-859a-bee7bf421446/contexts {}
    info: [debug] Getting a list of available webviews
    info: [debug] executing cmd: C:\Android\AndroidSDK\sdk\platform-tools\adb.exe -s
     05fee1c825246224 shell "cat /proc/net/unix"
    info: [debug] Available contexts: NATIVE_APP
    info: [debug] []
    info: [debug] Available contexts: NATIVE_APP
    info: [debug] Responding to client with success: {"status":0,"value":["NATIVE_AP

Hi Fayizkc,

Initialize Android Driver first:
public static AndroidDriver android_driver = null;

Try this for Android:

Set contexts = android_driver.getContextHandles();
for(String s:contexts)
System.out.println(“Mobile Web View found”);


My app consists of multiple webviews and native views coming in random order. So this is the logic I’m using, which takes the last webview. This is for both iOS and Android

public void switchToWebView() {
    Set<String> contextNames = getAppiumWebDriver().getContextHandles();
    String lastestContextView = (String) contextNames.toArray()[contextNames.size()-1];

    if (lastestContextView.contains("WEBVIEW_") || lastestContextView.contains("WEBVIEW_com.xxxxxx.android.mobile.xxxxx")){


I am also facing the same issue, not able to switch to Web Context.
Please suggest what can be done.
Using; Java Client 2.1.0 and AndroidDriver

To switch between contexts in appium versions < 1.4.3 I used in C#:

var contextNames = driver.GetContexts();
driver.SetContext(contextNames[1]; ///for webview_1, for native_view 0

for the latest appium I had to change this code to only:
driver = new AndroidDriver<AppiumWebelement)(new Uri(“”), capabilities);
…some code here

driver.Context = “NATIVE_APP”;

so in your case it would be driver.Context = “WEBVIEW_1”;

hope this helps…took me a day to figure out the code change. :slight_smile:

Hi Lurita

Thank you for sharing the approach. Can you please share the complete method to open a webpage on real device Iphone using c#.

I am able to open the safari browser but not able to navigate to the desired webpage on the active browser.