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)
相关推荐
Awesome Baron12 小时前
skill、tool calling、MCP区别
开发语言·人工智能·python
测试员周周12 小时前
【AI测试系统】第4篇:告别硬编码!基于 Markdown + Python 的 Skill 引擎设计:让 AI 测试系统拥有无限扩展的“灵魂”
人工智能·python·测试
武帝为此12 小时前
【Selenium 屏幕截图】
python·selenium·测试工具
念恒1230613 小时前
Python(列表进阶)
python·学习
276695829213 小时前
阿里最新acw_sc__v2 分析
开发语言·python·acw_sc__v2·acw_sc__v2逆向·acw_sc__v2算法·acw_sc__v2算法分析·cookie逆向
vortex514 小时前
python 库劫持:原理、利用与防御
python·网络安全·提权
捉鸭子14 小时前
某音a_bogus vmp逆向
爬虫·python·web安全·node.js·js
曲幽15 小时前
FastAPI 生产环境静态文件完全指南:从 /favicon.ico 404 到 HSTS 混合内容,一次全根治
python·fastapi·web·static·media·404·hsts·favicon·url_for
Dontla15 小时前
Python asyncpg库介绍(基于Python asyncio的PostgreSQL数据库驱动)连接池、SQLAlchemy
数据库·python·postgresql
zh15702315 小时前
如何编写动态SQL存储过程_使用sp_executesql执行灵活查询
jvm·数据库·python