python+appium在使用swipe滑动时,报错The swipe did not complete successfully的解决办法

博客主要讲述学习Python+Appium的滑动操作swipe时遇到报错,错误信息为selenium.common.exceptions.WebDriverException。解决办法是在每次swipe前添加强制等待1s,即添加sleep(1)代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在学习python+appium的滑动操作——swipe 时,运行的时候报错:
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: The swipe did not complete successfully

解决方法:只需在每次swipe之前,就使用强制等待1s即可,即添加一行 sleep(1) ,如下代码:

from time import sleep

from appium import webdriver
from selenium.webdriver.common.by import By

# 定义字典变量
desired_caps = {}
# 字典追加启动参数
desired_caps["platformName"] = "Android"
# 注意:版本号必须正确
desired_caps["platformVersion"] = "5.1.1"
# android不检测内容,但是不能为空
desired_caps["deviceName"] = "192.168.56.101:5555"
desired_caps["appPackage"] = "com.netease.newsreader.activity"
desired_caps["appActivity"] = "com.netease.nr.phone.main.MainActivity"
# 设置中⽂
desired_caps["unicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True
# 获取driver
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 练习二:打开手机《网易新闻》应用,完成下面的步骤:
# ①.模拟手机滑动频道,找到‘健康’频道,并点击
# com.netease.newsreader.activity/com.netease.nr.phone.main.MainActivity
driver.implicitly_wait(10)
driver.find_element(By.ID,"com.netease.newsreader.activity:id/bkj").click()

i = 0
while i < 5:
    sleep(1) #每次swipe之前,强制等待1s
    driver.swipe(900, 200, 50, 200, duration=2000)
    i += 1
driver.find_element(By.XPATH,"//*[@text='健康']").click()

sleep(3)
driver.quit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值