【appium】解決 打開第二個webview識別第一個webview元素

問題:python

 hybrid app進行appium自動化時,都會進行contexts切換到webview來進行事件點擊。但若是打開了第二個webview的狀況下而且webview是新開頁面,可能會識別成以前的webview內容。致使沒法進行事件點擊。web

例:chrome

 

解決方法:bash

 切換到新的webview頁面時候,先進行切換回native的context,此時殺掉chromedriver進行,再次切換webview context讓chromedriver從新獲取新的webview。app

注意:不要在webview context狀況下殺掉chromedriver,會致使頁面關閉。blog

 

更新:appium1.5版本以上能夠設置事件

recreateChromeDriverSessions=true  不須要手動kill。

 

 

殺掉chromedriver:webview

pkill -9 chromedriver

 

 

附帶切換context代碼it

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
相關文章
相關標籤/搜索