Switch to WebView using getcontexthandles()

I got red errors for when i am using getcontextHandles() for switch to WebView,Can you please suggest me what package i need to import?

It’s a method in driver class. What do you mean by red error. Are you using the statement like below

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