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)
相关推荐
Mike_6663 小时前
txt_json和xml_json
xml·python·json
zyq99101_13 小时前
DFS算法实战:经典例题代码解析
python·算法·蓝桥杯·深度优先
数据知道3 小时前
claw-code 源码分析:从 TypeScript 心智到 Python/Rust——跨栈移植时类型、边界与错误模型怎么对齐?
python·ai·rust·typescript·claude code·claw code
hhh3u3u3u4 小时前
Visual C++ 6.0中文版安装包下载教程及win11安装教程
java·c语言·开发语言·c++·python·c#·vc-1
好家伙VCC4 小时前
**发散创新:基于Python与ROS的机器人运动控制实战解析**在现代机器人系统开发中,**运动控制**是实现智能行为的核心
java·开发语言·python·机器人
2401_827499994 小时前
python项目实战09-AI智能伴侣(ai_partner_2-3)
开发语言·python
派葛穆4 小时前
汇川PLC-Python与汇川easy521plc进行Modbustcp通讯
开发语言·python
代码小书生5 小时前
Matplotlib,Python 数据可视化核心库!
python·信息可视化·matplotlib
默 语5 小时前
Records、Sealed Classes这些新特性:Java真的变简单了吗?
java·开发语言·python