Switch to webview

Hello

Help please i have to test our android app which contain a webview to log then when trying to switch to webview i can’t :

my code is

import time
from appium import webdriver

desired_caps = {}
desired_caps[‘platformName’] = ‘Android’
desired_caps[‘platformVersion’] = ‘12’
desired_caps[‘deviceName’] = ‘OPPO Find’
desired_caps[‘udid’] = ‘6da3aaef’
desired_caps[‘appPackage’] = ‘’
desired_caps[‘appActivity’] =’’
desired_caps[‘autoWebview’] = ‘false’

driver =webdriver.Remote(“http://127.0.0.1:4723/wd/hub”,desired_caps)

to wait

driver.implicitly_wait(30)

driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.Button’).click()

driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.Button’).click()

driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.Button’).click()

driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[1]’).click()

driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[1]’).click()

driver.find_element_by_xpath(’/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.Button[1]’).click()

to automate webview

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_by_class_name(“login_submit”).click login_usesso

#driver.find_element_by_id(“login_usesso”).click
driver.find_element_by_id(“login_forgot_password”).click

switch back to native view

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

do more native testing if we want

#driver.quit()

[W3C (f96bd340)] Calling AppiumDriver.getContexts() with args: [“f96bd340-73f2-4a5a-ac91-3538bc78cd6c”]
[AndroidDriver] Getting a list of available webviews
[ADB] Running ‘/home/nada/Android/Sdk/platform-tools/adb -P 5037 -s 6da3aaef shell cat /proc/net/unix’
[AndroidDriver] Found no active devtools sockets
[AndroidDriver] Other sockets are: [
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 84072 @oiface”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 84068 @resmon”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 86596 @rp_socket_server”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 80275 @reslog”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 79744 @hans_socket_server”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 15685 @time_genoff”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 59747 @criticallog_socket”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 31455520 @6f6dab3e-3634-4864-ba6b-c76f3786702e”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 12241919 @com.android.internal.os.WebViewZygoteInit/9b283708-702a-43c6-99c8-a7da14e5f763”,
[AndroidDriver] “0000000000000000: 00000002 00000000 00010000 0001 01 14134 @THERMALE_UI”,

If a web view is not picked up by Chrome remote debugger then there’s no chance you could switch to it from Appium.