Selenium 滑动 vs Appium 滑动

一、核心区别总结

维度 Selenium (Web端) Appium (移动端)
操作对象 浏览器页面 手机屏幕/应用
滑动方式 滚动到元素位置 基于坐标或元素滑动
常用方法 execute_script() 执行 JS swipe()scroll()drag_and_drop()
手势支持 简单滚动 支持复杂手势(长按、拖拽、多指操作)

二、Selenium 滑动实现

1. 滚动到指定元素(最常用)
复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
​
driver = webdriver.Chrome()
​
# 滚动到元素可见位置
element = driver.find_element(By.ID, "footer")
driver.execute_script("arguments[0].scrollIntoView();", element)
2. 滚动到页面底部/顶部
复制代码
# 滚动到底部
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
​
# 滚动到顶部
driver.execute_script("window.scrollTo(0, 0);")
3. 按像素滚动
复制代码
# 向下滚动500像素
driver.execute_script("window.scrollBy(0, 500);")
4. 使用 ActionChains(模拟鼠标拖拽滚动)
复制代码
from selenium.webdriver.common.action_chains import ActionChains
​
element = driver.find_element(By.ID, "scrollable-div")
ActionChains(driver).move_to_element(element).click_and_hold().move_by_offset(0, -100).release().perform()

三、Appium 滑动实现

1. swipe() - 屏幕滑动(基于坐标)
复制代码
from appium import webdriver
​
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
​
# 从 (100, 1200) 滑动到 (100, 300),持续500毫秒
driver.swipe(start_x=100, start_y=1200, end_x=100, end_y=300, duration=500)
2. scroll() - 元素间滑动
复制代码
# 从元素A滑动到元素B(直到元素B可见)
driver.scroll(origin_el=element_from, destination_el=element_to)
3. drag_and_drop() - 拖拽元素
复制代码
# 将元素A拖拽到元素B的位置
driver.drag_and_drop(element_from, element_to)
4. TouchAction - 高级手势(推荐)
复制代码
from appium.webdriver.common.touch_action import TouchAction
​
# 模拟手指滑动
TouchAction(driver).press(x=100, y=1200).wait(200).move_to(x=100, y=300).release().perform()
​
# 长按并移动
TouchAction(driver).long_press(el=element).move_to(el=target_element).release().perform()
5. 滑动到指定元素(封装常用)
复制代码
def scroll_to_element(driver, text):
    """滑动直到找到包含指定文本的元素"""
    element = driver.find_element(By.XPATH, f"//*[contains(@text, '{text}')]")
    driver.execute_script("arguments[0].scrollIntoView();", element)  # 部分App支持
    # 或使用 Appium 的 scroll
    driver.scroll(element)

四、完整对比示例

Selenium 示例(Web端)
复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()
driver.get("https://www.example.com")
driver.maximize_window()

# 滚动到底部
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)

# 滚动到某个元素
footer = driver.find_element(By.TAG_NAME, "footer")
driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'center'});", footer)
time.sleep(2)

driver.quit()
Appium 示例(移动端)
复制代码
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time

caps = {
    "platformName": "Android",
    "deviceName": "emulator",
    "appPackage": "com.android.settings",
    "appActivity": ".Settings"
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
time.sleep(3)

# 1. 基于坐标滑动(从下往上)
driver.swipe(500, 1500, 500, 500, 500)

# 2. 使用 TouchAction 滑动
TouchAction(driver).press(x=500, y=1500).wait(300).move_to(x=500, y=500).release().perform()

# 3. 元素间滑动(从WLAN滑动到更多)
wlan = driver.find_element(By.XPATH, "//*[@text='WLAN']")
more = driver.find_element(By.XPATH, "//*[@text='更多']")
driver.scroll(wlan, more)

time.sleep(2)
driver.quit()

五、选择建议

场景 推荐方案
Web端滚动到元素 execute_script("arguments[0].scrollIntoView()", element)
Web端滚动固定像素 execute_script("window.scrollBy(0, 500)")
移动端上下滑动屏幕 driver.swipe() 基于坐标,坐标大小不超过分辨率
移动端元素间滑动 driver.scroll()TouchAction
移动端复杂手势 TouchAction(长按、拖拽、多点触控)

六、常见问题

问题 Selenium Appium
滑动后找不到元素 需要等待页面加载 可能需要增加 duration 或使用 scroll()
坐标定位不准 不适用 使用 driver.get_window_size() 动态计算坐标
滑动太快 无法控制速度 调整 duration 参数(毫秒)

Appium 动态计算滑动坐标示例:

复制代码
size = driver.get_window_size()
start_x = size['width'] // 2
start_y = int(size['height'] * 0.8)
end_y = int(size['height'] * 0.2)
​
driver.swipe(start_x, start_y, start_x, end_y, 800)
相关推荐
love530love4 小时前
OpenClaw Windows Companion 桌面客户端 连接 LM Studio 完整配置指南
人工智能·windows·python·openclaw
cui_ruicheng6 小时前
Python从入门到实战(十六):多进程编程
开发语言·python
_Jimmy_8 小时前
FastAPI + SQLAlchemy全局事务管理
python·fastapi
用户8356290780518 小时前
如何使用 Python 在 Excel 中添加、编辑和删除超链接
后端·python
AC赳赳老秦10 小时前
企业工商公开信息采集分析:OpenClaw 批量查询企业工商信息,生成企业画像报告
大数据·开发语言·python·自动化·php·deepseek·openclaw
FoldWinCard10 小时前
D6 Python 基础语法 --- 保留关键字
开发语言·python
暮暮祈安11 小时前
Celery 新手入门指南
java·数据库·python·flask·httpx
JustNow_Man11 小时前
每日高频场景英文口语
python
风吹心凉11 小时前
python3基础2026.7.22
开发语言·python
小猴子爱上树13 小时前
一站式解决图片视频翻译难题的高效AI工具
人工智能·python·音视频