Can't switch context to webview

unable to switch to Webview with appium version 1.4.11 or 1.4.12 when test with hybrid apps on Android 4.4.2 or 5.0.1
any idea ? is it the bug in this release?

info: [debug] Getting a list of available webviews
info: [debug] executing cmd: C:\Users\xxxx\AppData\Local\Android\sdk1\platform-tools\adb.exe -s
05e1839607a3b748 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 error: {“status”:35,“value”:{“message”:“No such context foun
d.”,“origValue”:“Context ‘WEBVIEW_1’ does not exist”},“sessionId”:“7bf2552a-995f-402e-ab71-26d4e25ab
f95”}

Hello @maruko888 have you found any solution for your problem ?
I’m having the exact same issue.

@maruko888 @anapadinha

I am using appium version 1.4.14 to test a hybrid app developed by Ionic. The same error message shows up when switching to webview action is executed:

nfo: [debug] Getting a list of available webviews
info: [debug] executing cmd: /Users/gr_haonan/Library/Android/sdk/platform-tools/adb -s 73f50e0d 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 error: {“status”:35,“value”:{“message”:“No such context found.”,“origValue”:“Context ‘WEBVIEW_1’ does not exist”},“sessionId”:“0d4ff72b-8084-4546-ba8b-e2311ed05474”}

the code in python is in below as copied from the appium document:
context_name = “WEBVIEW_1”
driver.switch_to.context(context_name)

hi
Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {

	      System.out.println(contextNames);
	  }

this above line will o/p like[NATIVE_APP, WEBVIEW_com.xxx.xxxx.xxxx]

then you have switch the driver to needed one by using the line driver.context(“WEBVIEW_com.xxx.xxxx.xxxx”);

hi
Set contextNames = driver.getContextHandles();
for (String contextName : contextNames) {

      System.out.println(contextNames);
  }

this above line will give o/p like[NATIVE_APP, WEBVIEW_com.xxx.xxxx.xxxx]

then you have switch the driver to needed one by using the line driver.context(“WEBVIEW_com.xxx.xxxx.xxxx”);

Hi,
Please make sure that the version of selenium-server-standalone, selenium-java, java-client.
I used them as below:
selenium-server-standalone-2.48.2.jar
selenium-java-2.48.2.jar
java-client-3.2.0.jar

My code as below:
protected void switchToWebview() throws Exception {

	// E.G.['NATIVE_APP', 'WEBVIEW_1', ...]
	Set<String> contexts = driver.getContextHandles();

	// make sure we have something other than the native context
	// assertThat(contexts.size(), greaterThan(1));
	for (String context : contexts) {
		// System.out.println(contexts);
		if (!context.equals("NATIVE_APP")) {
			driver.context((String) contexts.toArray()[1]);
			break;
		}
	}
	System.out.println("execute context switch successfully!!!  "
			+ contexts.toArray()[1]);
}

@liucong_3689 I am also trying to automate hybrid iOS app build in ionic using appium only. Using c# for coding, I am able to switch the context but I am not able to identify elements after that. Have you got any solution for this or you made progress on this one.

Thanks in advance

I am using Appium 1.5 for Android.
I am able to successfully switch context to webview and interact with webview element ( my test broke previously when upgraded to 1.4 , 1.5 ) Heres the fixed snippet:

def switch_to_webview(driver):
	if not driver.context or driver.context == "NATIVE_APP":
		debug_log(“print your available contexts:  .................... %s”, driver.contexts)
		switchto = driver.contexts[0]    //point to the index of your webview
		driver.switch_to.context(switchto)
	else:
		debug_log("no need to switch")

self.webview = lambda: self.driver.find_element_by_id("webview")
self.webview()
switch_to_webview(self.driver)

I was automating an hybrid app, while switching, it only shows name of native context. WebView Context is shown as webview_undefined. So how can I get all the webview context names ? pls help soon

I believe java libraries have this issue , however I recently saw python client libraries show all contexts in robust way

do you know which libraries are required for it ?

  • Even i am facing the same problem.i am using android version 6.0.1 and appium version 1.4.16.1.But driver.context() method is failing to switch to webview.I have added chromedriverHandler().jar but still facing the problem.

switching to webview is working only for few times.In remaining all the cases it fails to switch.Any answers could be helpful

Context switching is happening only at few times.Consistency is not there.what could be the solution?

Go to Safari>Advanced>WebInspector>ON
Should work.

Even i am facing same issue , Please let me know if any solution.

Even I am facing the same issue using java.When I tried to switch to webview ,I am getting the context as undefined.Some times it is printing webview context as "WEBVIEW_undefined "and some times “WEBVIEW_XXXXX”.Can any one please help me how to resolve it.

I am still facing same issue with Appium Java Client 4.1.2 and Appium @1.6.5 for IOS

[debug] [XCUITest] Executing command ‘setContext’
[debug] [iOS] Attempting to set context to ‘WEBVIEW_1’
[HTTP] <-- POST /wd/hub/session/bb96e125-75f3-4df1-8859-60692ebe7ac3/context 500 3 ms - 109
[HTTP] --> GET /wd/hub/session/bb96e125-75f3-4df1-8859-60692ebe7ac3/contexts {}
[debug] [MJSONWP] Calling AppiumDriver.getContexts() with args: [“bb96e125-75f3-4df1-8859-60692ebe7ac3”]
[debug] [XCUITest] Executing command ‘getContexts’
[debug] [iOS] Getting list of available contexts
[debug] [iOS] Retrieving contexts and views
[debug] [iOS] Selecting by url: false
[debug] [RemoteDebugger] Getting WebKitRemoteDebugger pageArray: localhost, 27753
[debug] [RemoteDebugger] Sending request to: http://localhost:27753/json
[debug] [RemoteDebugger] Page element JSON: []
[debug] [iOS] No web frames found.
[debug] [MJSONWP] Responding to client with driver.getContexts() result: [“NATIVE_APP”]
[HTTP] <-- GET /wd/hub/session/bb96e125-75f3-4df1-8859-60692ebe7ac3/contexts 200 15 ms - 86
[HTTP] --> GET /wd/hub/session/bb96e125-75f3-4df1-8859-60692ebe7ac3/screenshot {}
[debug] [MJSONWP] Calling AppiumDriver.getScreenshot() with args: [“bb96e125-75f3-4df1-8859-60692ebe7ac3”]
[debug] [XCUITest] Executing command ‘getScreenshot’
[debug] [JSONWP Proxy] Proxying [GET /screenshot] to [GET http://localhost:8100/session/7C9D2E75-E54E-466C-A4F0-C37546B7EF53/screenshot] with no body
[debug] [JSONWP Proxy] Got response with status 200: "{\n “value” : "iVBORw0KGgoAAAANSUhEUgAABNoAAAigCAYAAADpkk+ZAAAAAXNSR0IArs4c6QAA\r\nABxpRE9UAAAAAgAAAAAAAARQAAAAKAAABFAAAARQAAHszMNSqAcAAEAASURBVHgB\r\n7J0H2BxVvf9BBQvYrwW8Vy5Fr4oFFP2rKOpVxIJ6LaiAIiA2BBtiAyWEAAkQSihJ\r\nqAlJSO8QIJRASCWNhBBCOoTeewkk5z/fiWdz9uzM7Mzu7O7s7ud9nveZsnPa73xm\r\nzpnv/M45Wz3//AuGf2wAAzAAAzAAAzAAAzAAAzAAAzAAAzAAAzAAA/UxsBUGrM+A\r\n2A/7wQAMwAAMwAAMwAAMwAAMwAAMwAAMwAAMiAGENjz68GiEARiAARiAARiAARiA\r\nARiAARiAARiAARjIgQGEthyMiGqNag0DMAADMAADMAADMAADMAADMAADMAADMIDQ\r\nhtCGYg0DMAADMAADMAADMAADMAADMAADMAADMJADAwhtORgRxRrFGgZgAAZgAAZg\r\nAAZgAAZgAAZgAAZgAAZgAKENoQ3FGgZgAAZgAAZgAAZgAAZgAAZgAAZgAAZg4PkX\r\nAxtU+08WExHauJG4kWAABmAABmAABmAABmAABmAABmAABmCgwxiQYJYsipX/Xk1g\r\n83+PjhuhLZPRo41YXjFcgz1gAAZgAAZgAAZgAAZgAAZgAAZgAAZgoHUMuKJYmnpw\r\nr8+yXxk3QhtCW0aFtxKi1t045AXbwwAMwAAMwAAMwAAMwAAMwAAMwAAMuAxECWXu\r\n7/5+1PVZzpXHh9CG0IbQBgMwAAMwAAMwAAMwAAMwAAMwAAMwAAMdwECSQFYuiG0R\r\nJ5PCpPmtPF6ENm6kDriRyqHecrNwHlvAAAzAAAzAAAzAAAzAAAzAAAzAQHcwkF0U\r\n22yXN…
[debug] [MJSONWP] Responding to client with driver.getScreenshot() result: "iVBORw0KGgoAAAANSUhEUgAABNoAAAigCAYAAADpkk+ZAAAAAXNSR0IArs4c6QAA\r\nABxpRE9UAAAAAgAAAAAAAARQAAAAKAAABFAAAARQAAHszMNSqAcAAEAASURBVHgB\r\n7J0H2BxVvf9BBQvYrwW8Vy5Fr4oFFP2rKOpVxIJ6LaiAIiA2BBtiAyWEAAkQSihJ\r\nqAlJSO8QIJRASCWNhBBCOoTeewkk5z/fiWdz9uzM7Mzu7O7s7ud9nveZsnPa73xm\r\nzpnv/M45Wz3//AuGf2wAAzAAAzAAAzAAAzAAAzAAAzAAAzAAAzAAA/UxsBUGrM+A\r\n2A/7wQAMwAAMwAAMwAAMwAAMwAAMwAAMwAAMiAGENjz68GiEARiAARiAARiAARiA\r\nARiAARiAARiAARjIgQGEthyMiGqNag0DMAADMAADMAADMAADMAADMAADMAADMIDQ\r\nhtCGYg0DMAADMAADMAADMAADMAADMAADMAADMJADAwhtORgRxRrFGgZgAAZgAAZg\r\nAAZgAAZgAAZgAAZgAAZgAKENoQ3FGgZgAAZgAAZgAAZgAAZgAAZgAAZgAAZg4PkX\r\nAxtU+08WExHauJG4kWAABmAABmAABmAABmAABmAABmAABmCgwxiQYJYsipX/Xk1g\r\n83+PjhuhLZPRo41YXjFcgz1gAAZgAAZgAAZgAAZgAAZgAAZgAAZgoHUMuKJYmnpw\r\nr8+yXxk3QhtCW0aFtxKi1t045AXbwwAMwAAMwAAMwAAMwAAMwAAMwAAMuAxECWXu\r\n7/5+1PVZzpXHh9CG0IbQBgMwAAMwAAMwAAMwAAMwAAMwAAMwAAMdwECSQFYuiG0R\r\nJ5PCpPmtPF6ENm6kDriRyqHecrNwHlvAAAzAAAzAAAzAAAzAAAzAAAzAQHcwkF0U\r\n22yXNOGSrinnC6ENoQ2hDQZgAAZgAAZgAAZgAAZgAAZgAAZgAAbamIEkIcz9rVwU\r\n…

This discussion was long time ago, but I have the same problem with the latest Appium 1.21.0. I can get the list using getContextHandles():
[“NATIVE_APP”,“WEBVIEW_XX.XXXXX.XXXX”]
But when I switch to webview using getDriver().context(WEBVIEW_XX.XXXXX.XXXX), it throws exception from time to time. This line of code is very unstable.
I was surprised that this problem wasn’t resolved.

I still having same issue in Android real devices, no updates so far?