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)
相关推荐
北小菜14 小时前
xclabel是一款开源图像标注与模型训练工具,采用Python+Flask开发,跨平台支持Windows/Linux/Mac
python·神经网络·计算机视觉·labelme·视频行为分析系统
徐安安ye14 小时前
FlashAttention在昇腾NPU上的性能实测:数据、瓶颈与优化上限
python·transformer
YsyaaabB14 小时前
ACM 模式通用代码模板
java·c++·python·算法
hnxaoli14 小时前
统信小程序(十三)循环键鼠操作程序
python·小程序
AI视觉网奇14 小时前
blender底部对齐
开发语言·python·blender
lunzi_082614 小时前
【学习笔记】《Python编程 从入门到实践》第2章:变量命名规则、字符串操作与数值类型详解
笔记·python·学习
E_ICEBLUE14 小时前
Python 教程:快速复制 Excel 工作表
python·excel
是三旬老汉。14 小时前
宇树G1-D机器人端推理开发记录
python·机器人
databook14 小时前
轨迹的蓝图:方程求解与交点计算
python·数学·动效
隔壁大炮14 小时前
MNE-Python 第1天学习笔记:环境搭建与数据初探
python·eeg·bci·mne·脑电数据处理