問題:
hybrid app進行appium自動化時,都會進行contexts切換到webview來進行事件點擊。但如果打開了第二個webview的情況下並且webview是新開頁面,可能會識別成之前的webview內容。導致無法進行事件點擊。
例:

解決方法:
切換到新的webview頁面時候,先進行切換回native的context,此時殺掉chromedriver進行,再次切換webview context讓chromedriver重新獲取新的webview。
注意:不要在webview context情況下殺掉chromedriver,會導致頁面關閉。
更新:appium1.5版本以上可以設置
recreateChromeDriverSessions=true 不需要手動kill。
殺掉chromedriver:
pkill -9 chromedriver
附帶切換context代碼
def switch_context(self, context="WEBVIEW_xxxx"):
context = context.upper()
contexts = self.driver.contexts
for con in contexts:
if context in con and context.find('WEBVIEW') != -1:
print 'switch webview'
self.driver.switch_context(con)
res = self.driver.current_context
return True if context in res else False
elif context in con and context.find('NATIVE') != -1:
print 'switch native'
self.driver.switch_context(con)
os.system('pkill -9 chromedriver')
res = self.driver.current_context
print res
return True if context in res else False
return SwitchContextError
def switch_content_default(self):
time.sleep(3)
self.driver.switch_context('NATIVE_APP')
os.system('pkill -9 chromedriver')
for con in self.driver.contexts:
if con.find('WEBVIEW') != -1:
self.driver.switch_context(con)
return True
return SwitchContextError
