Appium 请问下大佬们,python +appium 运行移动端脚本加载很慢,怎么去优化

变成风才和我相遇 · 2023年05月09日 · 最后由 变成风才和我相遇 回复于 2023年05月17日 · 7631 次阅读
time.sleep(5)
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)


# 设置搜过为默认输入法
# 'com.sohu.inputmethod.sogouoem/.SogouIME'


def swipeUp(driver, t=500, n=1):
    """向上滑动屏幕"""
    l = driver.get_window_size()
    x1 = l['width'] * 0.75  # x坐标
    y1 = l['height'] * 0.75  # 起始y坐标
    y2 = l['height'] * 0.25  # 终点y坐标
    for i in range(n):
        driver.swipe(x1, y1, x1, y2, t)


def swipeDown(driver, t=500, n=1):
    """向下滑动屏幕"""
    l = driver.get_window_size()
    x1 = l['width'] * 0.5  # x坐标
    y1 = l['height'] * 0.25  # 起始y坐标
    y2 = l['height'] * 0.75  # 终点y坐标
    for i in range(n):
        driver.swipe(x1, y1, x1, y2, t)


def swipLeft(driver, t=500, n=1):
    """向左滑动屏幕"""
    l = driver.get_window_size()
    x1 = l['width'] * 0.75
    y1 = l['height'] * 0.5
    x2 = l['width'] * 0.25
    for i in range(n):
        driver.swipe(x1, y1, x2, y1, t)


def swipRight(driver, t=500, n=1):
    """向右滑动屏幕"""
    l = driver.get_window_size()
    x1 = l['width'] * 0.25
    y1 = l['height'] * 0.5
    x2 = l['width'] * 0.75
    for i in range(n):
        driver.swipe(x1, y1, x2, y1, t)


# 去除弹窗
time.sleep(5)
# el1 = driver.find_element(By.ID, "com.changsha.apps.android.mycs:id/iv_close")
# el1.click()
# print("去除弹窗", el1)

# 进入话题广场页面
time.sleep(5)
el1 = driver.find_element(By.ID, 'com.changsha.apps.android.mycs:id/tv_interaction')
el1.click()
print("是否进入话题广场页面", el1)
# 下滑4,5,6,7,8次
time.sleep(5)
swipeUp(driver, n=8)
# 关注用户
# el2 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
#                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
#                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
#                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
#                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
#                                    ".View[2]/android.view.View[2]/android.widget.Button")
# el2 = driver.find_element(By.CLASS_NAME, "//android.widget.Button[@text='关注']")
time.sleep(10)
driver.implicitly_wait(10)
el2 = driver.find_element(By.XPATH, '//android.widget.Button')
# el2 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
#                                     ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
#                                     ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
#                                     "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
#                                     "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
#                                     ".View[2]/android.view.View[2]/android.widget.Button")
el2.click()
# # el2 = driver.find_element_by_xpath("//android.widget.Button[@text='关注']")
# el2 = driver.find_element(By.,"//android.widget.Button[@text='关注']")
# el2.click()
print("关注用户", el2)
# 点击收藏
time.sleep(5)
# el3 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
#                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
#                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
#                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
#                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
#                                    ".View/android.view.View[2]/android.widget.TextView[4]")
el3 = driver.find_element(By.XPATH, "hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
                                    ".View/android.view.View[2]/android.widget.TextView[4]")
el3.click()
print("点击收藏", el3)

# 点击评论 进入评论页面
time.sleep(5)
# el4 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
#                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
#                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
#                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
#                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
#                                    ".View/android.view.View[2]/android.widget.TextView[3]")
el4 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
                                    ".View/android.view.View[2]/android.widget.TextView[3]")
el4.click()
print("进入评论页面", el4)
# 点击写评论 弹出评论框
time.sleep(5)
# el5 = driver.find_element(By.XPATH, "//android.widget.TextView[@text='评论']")
# el5.click()

el5 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
                                    ".View[2]/android.view.View[1]")
# el5 = driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
#                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
#                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
#                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
#                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
#                                    ".View[2]/android.view.View[1]/android.widget.TextView[2]")
el5.click()
print("点击弹窗,弹出评论框", el5)
time.sleep(5)
el6 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
                                    ".View[2]/android.view.View[1]")
el6.click()

# 编写评论(存在senks 输入不了的问题)
# 实现手机键盘自带搜索操作
text = "hello world "
os.system(f"adb shell input text '{text}'")
print("键盘输入的信息", text)
# 评论用户动态,点击发布按钮
time.sleep(5)
el7 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
                                    "/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout"
                                    "/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view"
                                    ".View[2]/android.view.View[4]")
el7.click()
print("评论用户成功", el7)
# 关闭 窗口 返回到首页
# 关闭评论窗口
time.sleep(5)
a = TouchAction(driver).tap(x=987, y=731).perform()
print("关闭评论窗口", a)

# 返回到话题广场首页
time.sleep(5)
# el8 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
#                                     ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget"
#                                     ".FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager/android"
#                                     ".view "
#                                     ".ViewGroup/android.view.ViewGroup/android.widget.ImageView")

el8 = driver.find_element(By.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget"
                                    ".FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android"
                                    ".widget.FrameLayout/android.view.ViewGroup/androidx.viewpager.widget.ViewPager"
                                    "/android.view.ViewGroup/android.view.ViewGroup/android.widget.ImageView")
el8.click()
print("返回到话题广场首页", el8)
if __name__ == '__main__':
    print(driver.get_window_size())

```py

问怎么优化appium 移动端代码
共收到 9 条回复 时间 点赞

没贴代码没法给出建议。可以尝试使用 pytest 的 pytest-xdist 插件去实现多机并行。

我也发现这个问题了,我用的原生的 uiautomator 元素定位很快

可以尝试用 uiautomator2 这个库去驱动设备,很快而且很准,里面也有一些保障容错的小方法

4楼 已删除

我去试试你门推荐

叶芜 回复

代码已经贴上

jack 回复

我去试试

好勒,谢谢

  1. 能用 id 尽量用 id,不能用就是用 xpath(//*[@text='需要检测的文字']),用 full xpath 肯定很慢,而且不易维护
  2. 当页面存在动图或者视频时一定加上 des 参数 "waitForIdleTimeout": 100,
  3. 选择一个稳定的 appium 版本,我用的 1.21.0,稳如老狗 基本满足上面在 android 端不会执行的很慢,ios 另说
Time 回复

谢谢大佬的指导,我立马去试试

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册