App 启动后弹出权限窗处理

这种弹窗是可以直接定位到的,这里不能通过id去定位了,因为弹窗比较多,每次id都不太一样,但是文本text都是一样的:始终允许

from
appium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import os import time

1 通过xpath来定位按钮:始终允许


loc = ("xpath", "//*[@text='始终允许']")


2.用selenium里面的显示等待模块(WebDriverWait)和判断模块(expected_conditions)封装定位方法,之前selenium教程里面详细讲过,这里就不重复写了


from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



PATH = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), x)


print(PATH('meituan_626.apk'))


desired_caps = {
                'platformName': 'Android',
                'deviceName': '127.0.0.1:62001',
                'platformVersion': '7.0',
                'app': PATH('meituan_626.apk'),
                'appPackage': 'com.sankuai.meituan',
                'appActivity': 'com.meituan.android.pt.homepage.activity.Welcome',
                'noReset': 'true',
                # 'resetKeyboard': 'true',
                # 'unicodeKeyboard': 'true'
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

time.sleep(3)

def always_allow(driver, number=5):
    '''
    fuction:权限弹窗-始终允许
    args:1.传driver
    2.number,判断弹窗次数,默认给5次
    其它:
    WebDriverWait里面0.5s判断一次是否有弹窗,1s超时
    '''
    for i in range(number):
        loc = ("xpath", "//*[@text='始终允许']")
        try:
            e = WebDriverWait(driver,5, 0.5).until(EC.presence_of_element_located(loc))
            e.click()
        except:
            pass

if __name__ == "__main__":
    # 调用始终允许函数
    always_allow(driver)
原文地址:https://www.cnblogs.com/lexus168/p/13942583.html