1、如何处理 android 权限弹窗
# 处理 android 权限弹窗 while True: for i in range(6): if '允许' in driver.page_source: driver.switch_to.alert.accept() time.sleep(1) break
2、如何处理UI自动化中不定时的弹窗,比如升级提示弹窗、营销弹窗、权限弹窗等
可以在脚本初始化时,启动一个线程来处理
def popup(driver): while True: # 权限弹窗 if '允许' in driver.page_source: driver.switch_to.alert.accept() # 其他弹窗 elements = [ (By.XPATH, '//android.widget.TextView[@text="哈哈"]'), (By.XPATH, '//android.widget.TextView[@text="去首页"]'), (By.XPATH, '//*[@text="暂时离开"]') ] for element in elements: try: driver.find_element(*element).click() except(Exception) as e: print(e, end='') time.sleep(1) print('...checking..popup...') time.sleep(1)
t = Thread(target=popup, args=(driver,)) t.start()
问题: 如何结束子线程(是否可以使用子进程呢)
通过进程来解决
使用Queue来进行进程间的通信, 思路: 脚本执行完成后, 发生消息给监听该队列的子进程, break掉循环
from multiprocessing import Queue from multiprocessing import Process q = Queue() def popup(driver, q): while True: # 权限弹窗 if '允许' in driver.page_source: driver.switch_to.alert.accept() # 其他弹窗 elements = [ (By.XPATH, '//android.widget.TextView[@text="哈哈"]'), (By.XPATH, '//android.widget.TextView[@text="去首页"]'), (By.XPATH, '//*[@text="暂时离开"]') ] for element in elements: try: driver.find_element(*element).click() except(Exception) as e: print(e, end='') time.sleep(1) print('...checking..popup...') time.sleep(1) # 获取到队列中有值, 则break循环 try: if q.get_nowait() == 'stop':break except: pass if __name__ == '__main__': p = Process(target=productor, args=(q,)) # 等待脚本运行完成, 往队列方式消息 q.put('stop')
3、如何识别NATIVE_APP中是否有H5页面
driver.contexts
可以识别当前也是的webview ,比如:['NATIVE_APP', 'WEBVIEW_com.android.browser', 'WEBVIEW_com.xx.xx']
4、如何设置appium不清除APP数据
# 解决Could not proxy. Proxy error: Could not proxy command to remote server. Original error: Error: socket hang up
desired_caps['recreateChromeDriverSessions'] = 'True'
desired_caps['noReset'] = 'True' # 不清除数据