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)
相关推荐
hhzz9 分钟前
【OpenCV 入门到精通 07】滤波、阈值与形态学:图像去噪与形状处理
人工智能·python·opencv·计算机视觉
步行cgn30 分钟前
Spring 注入 Properties 详解
java·python·spring
shirsl39 分钟前
算法 Day3-队列 / deque + 链表
数据结构·python·算法·链表
名字还没想好☜1 小时前
Python 的 __call__ 实战:让实例像函数一样被调用,做带状态计数器、缓存器与可配置策略
开发语言·后端·python·缓存·编程语言
cpolar技术支持1 小时前
AI Agent 跑半小时就忘目标?用检查点与任务账本做可恢复长任务,cpolar 分享只读时间线
python·sqlite·cpolar·ai agent·任务恢复
张小姐的猫1 小时前
【AI大模型接入SDK】 —— SQLite上手
linux·开发语言·c++·人工智能·python·log4j
529宝宝起名网1 小时前
用 Python 实现基于字源特征的名字文化评分算法:从六书部首到文化寓意的多维度评分模型
python
努力学习的代码小白1 小时前
mem0源码学习03
python
TomEval1 小时前
【性能测试】17 - 性能测试入门
测试工具·jmeter