Cannot switch from native to webview using python ubuntu

I am new to python, Below are the snippets we tried and failed to switch to web view from native view

  1. python

assuming we have an initialized driver object for an app

switch to webview

webview = driver.contexts.last
driver.switch_to.context(webview)

do some webby stuff

driver.find_element(:css, “.green_button”).click

switch back to native view

driver.switch_to.context(driver.contexts.first)

do more native testing if I want

driver.quit()
Result : Shows driver exception

  1. Can't switch context to webview
    Result : NoSuchContextException: Message: No such context found.

Is this the correct approach or is there any other way.Could anyone please help me if you have faced the same issue and resolved it.
Thanks in advance.

What device are you running the test on?

I’m having the same issue with Appium 1.6.3 on a real Android device (tested 6.0/7.0/7.1.1). It does however work on a simulator.

I am working on Samsung J7(6.0)
Tried on Simulator, was not able to switch to “WEBVIEW”. It says still in “NATIVE_APP”
Could you please share the code that you used to run it on the Emulator.

Thanks in advance.

I’m writing my tests in C#, so may not be much use to you i’m afraid!

Hey, did you get the solution by any chance? I’m stuck here as well.