Selenium 用户交互 #

交互操作概览 #

交互类型 #

text
┌─────────────────────────────────────────────────────────────┐
│                    Selenium 交互操作                         │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                    键盘操作                          │   │
│  ├─────────────────────────────────────────────────────┤   │
│  │  • 文本输入 (send_keys)                              │   │
│  │  • 特殊按键 (Keys 类)                                │   │
│  │  • 快捷键组合 (Ctrl+C, Ctrl+V 等)                    │   │
│  │  • 键盘事件 (key_down, key_up)                       │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                    鼠标操作                          │   │
│  ├─────────────────────────────────────────────────────┤   │
│  │  • 单击 (click)                                      │   │
│  │  • 双击 (double_click)                               │   │
│  │  • 右键点击 (context_click)                          │   │
│  │  • 悬停 (move_to_element)                            │   │
│  │  • 拖拽 (drag_and_drop)                              │   │
│  │  • 滚动 (scroll)                                     │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │                  复合操作                            │   │
│  ├─────────────────────────────────────────────────────┤   │
│  │  • ActionChains 链式操作                             │   │
│  │  • 多步骤组合                                        │   │
│  │  • 拖拽到偏移量                                      │   │
│  │  • 按住拖拽                                          │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

键盘操作 #

基本文本输入 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By

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

# 找到输入框
input_field = driver.find_element(By.ID, "username")

# 清空并输入
input_field.clear()
input_field.send_keys("Hello World")

# 追加输入
input_field.send_keys(" - Additional Text")

driver.quit()

特殊按键 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

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

input_field = driver.find_element(By.ID, "search")

# 常用特殊按键
input_field.send_keys("Selenium")
input_field.send_keys(Keys.ENTER)      # 回车
input_field.send_keys(Keys.TAB)        # Tab 键
input_field.send_keys(Keys.ESCAPE)     # ESC 键
input_field.send_keys(Keys.BACKSPACE)  # 退格
input_field.send_keys(Keys.DELETE)     # 删除
input_field.send_keys(Keys.SPACE)      # 空格

# 方向键
input_field.send_keys(Keys.ARROW_UP)
input_field.send_keys(Keys.ARROW_DOWN)
input_field.send_keys(Keys.ARROW_LEFT)
input_field.send_keys(Keys.ARROW_RIGHT)

# 功能键
input_field.send_keys(Keys.F1)
input_field.send_keys(Keys.F5)  # 刷新

driver.quit()

快捷键组合 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

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

input_field = driver.find_element(By.ID, "content")

# 全选 (Ctrl+A)
input_field.send_keys(Keys.CONTROL + 'a')

# 复制 (Ctrl+C)
input_field.send_keys(Keys.CONTROL + 'c')

# 粘贴 (Ctrl+V)
input_field.send_keys(Keys.CONTROL + 'v')

# 剪切 (Ctrl+X)
input_field.send_keys(Keys.CONTROL + 'x')

# 撤销 (Ctrl+Z)
input_field.send_keys(Keys.CONTROL + 'z')

# macOS 使用 COMMAND 键
input_field.send_keys(Keys.COMMAND + 'a')  # 全选
input_field.send_keys(Keys.COMMAND + 'c')  # 复制
input_field.send_keys(Keys.COMMAND + 'v')  # 粘贴

driver.quit()

使用 ActionChains 进行键盘操作 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

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

input_field = driver.find_element(By.ID, "content")
actions = ActionChains(driver)

# 按下按键
actions.key_down(Keys.SHIFT)
actions.send_keys("hello")  # 输出大写 HELLO
actions.key_up(Keys.SHIFT)
actions.perform()

# 组合键
actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

# 切换到元素并输入
actions.click(input_field).send_keys("Hello World").perform()

driver.quit()

鼠标操作 #

基本点击 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By

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

# 简单点击
button = driver.find_element(By.ID, "submit")
button.click()

# JavaScript 点击(适用于被遮挡的元素)
driver.execute_script("arguments[0].click();", button)

driver.quit()

使用 ActionChains 进行鼠标操作 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

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

element = driver.find_element(By.ID, "target")
actions = ActionChains(driver)

# 单击
actions.click(element).perform()

# 在当前位置单击
actions.click().perform()

# 双击
actions.double_click(element).perform()

# 右键点击
actions.context_click(element).perform()

driver.quit()

鼠标悬停 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

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

# 悬停触发下拉菜单
menu = driver.find_element(By.ID, "dropdown-menu")
actions = ActionChains(driver)

# 移动到元素
actions.move_to_element(menu).perform()

# 悬停后点击子菜单
submenu = driver.find_element(By.ID, "submenu")
actions.move_to_element(menu).move_to_element(submenu).click().perform()

driver.quit()

鼠标移动 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

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

element = driver.find_element(By.ID, "target")
actions = ActionChains(driver)

# 移动到元素中心
actions.move_to_element(element).perform()

# 移动到元素的偏移位置
actions.move_to_element_with_offset(element, 10, 20).perform()

# 从当前位置移动偏移量
actions.move_by_offset(100, 50).perform()

driver.quit()

拖拽操作 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("https://example.com/drag-drop")

source = driver.find_element(By.ID, "draggable")
target = driver.find_element(By.ID, "droppable")
actions = ActionChains(driver)

# 方式一:使用 drag_and_drop
actions.drag_and_drop(source, target).perform()

# 方式二:手动组合操作
actions.click_and_hold(source).move_to_element(target).release().perform()

# 拖拽到偏移位置
actions.drag_and_drop_by_offset(source, 100, 50).perform()

# 手动拖拽到偏移位置
actions.click_and_hold(source).move_by_offset(100, 50).release().perform()

driver.quit()

ActionChains 详解 #

工作原理 #

text
┌─────────────────────────────────────────────────────────────┐
│                  ActionChains 工作原理                       │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   actions = ActionChains(driver)                            │
│                                                             │
│   步骤 1: 添加操作到队列                                      │
│   actions.click(element)      ──> 队列: [click]             │
│   actions.send_keys("text")   ──> 队列: [click, send_keys]  │
│   actions.double_click()      ──> 队列: [click,             │
│                                          send_keys,         │
│                                          double_click]      │
│                                                             │
│   步骤 2: 执行队列中的所有操作                                 │
│   actions.perform()           ──> 按顺序执行所有操作          │
│                                                             │
│   步骤 3: 队列清空                                           │
│   队列: []                                                   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

链式调用 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

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

input_field = driver.find_element(By.ID, "input")
button = driver.find_element(By.ID, "submit")

# 链式调用 - 所有操作一次性执行
ActionChains(driver).click(input_field).send_keys("Hello").send_keys(Keys.ENTER).perform()

# 分步调用 - 每次调用 perform 都会执行
actions = ActionChains(driver)
actions.click(input_field)
actions.perform()  # 执行点击

actions.send_keys("World")
actions.perform()  # 执行输入

driver.quit()

复合操作示例 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

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

# 示例 1: 选择文本并复制
input_field = driver.find_element(By.ID, "content")
ActionChains(driver).click(input_field).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()

# 示例 2: 拖拽元素到目标位置后释放
source = driver.find_element(By.ID, "draggable")
target = driver.find_element(By.ID, "droppable")
ActionChains(driver).click_and_hold(source).move_to_element(target).pause(1).release().perform()

# 示例 3: 悬停后点击
menu = driver.find_element(By.ID, "menu")
submenu = driver.find_element(By.ID, "submenu")
ActionChains(driver).move_to_element(menu).pause(0.5).click(submenu).perform()

# 示例 4: 按住 Shift 输入大写字母
input_field = driver.find_element(By.ID, "input")
ActionChains(driver).click(input_field).key_down(Keys.SHIFT).send_keys("hello").key_up(Keys.SHIFT).perform()

driver.quit()

暂停和等待 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

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

element = driver.find_element(By.ID, "target")

# pause() - 在操作链中添加暂停(秒)
ActionChains(driver).click(element).pause(1).double_click(element).perform()

# 带暂停的复杂操作
ActionChains(driver).move_to_element(element).pause(0.5).click().pause(0.5).double_click().perform()

driver.quit()

滚动操作 #

页面滚动 #

python
from selenium import webdriver

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

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

# 滚动到顶部
driver.execute_script("window.scrollTo(0, 0);")

# 滚动指定距离
driver.execute_script("window.scrollBy(0, 500);")

# 滚动到指定位置
driver.execute_script("window.scrollTo(500, 300);")

driver.quit()

元素滚动 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By

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

element = driver.find_element(By.ID, "target")

# 滚动到元素可见
driver.execute_script("arguments[0].scrollIntoView();", element)

# 滚动到元素顶部对齐
driver.execute_script("arguments[0].scrollIntoView({block: 'start'});", element)

# 滚动到元素底部对齐
driver.execute_script("arguments[0].scrollIntoView({block: 'end'});", element)

# 滚动到元素居中
driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", element)

# 平滑滚动
driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth'});", element)

driver.quit()

水平滚动 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By

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

# 水平滚动容器
container = driver.find_element(By.ID, "scroll-container")

# 滚动到左侧
driver.execute_script("arguments[0].scrollLeft = 0;", container)

# 滚动到右侧
driver.execute_script("arguments[0].scrollLeft = arguments[0].scrollWidth;", container)

# 滚动指定距离
driver.execute_script("arguments[0].scrollLeft += 200;", container)

driver.quit()

触摸操作(移动端) #

基本触摸操作 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

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

element = driver.find_element(By.ID, "target")

# 使用 ActionChains 模拟触摸
actions = ActionChains(driver)

# 点击
actions.click(element).perform()

# 长按
actions.click_and_hold(element).pause(1).release().perform()

# 滑动
actions.click_and_hold(element).move_by_offset(100, 0).release().perform()

driver.quit()

移动端模拟 #

python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

# 设置移动端模拟
mobile_emulation = {
    "deviceName": "iPhone 12"
}

options = Options()
options.add_experimental_option("mobileEmulation", mobile_emulation)

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

# 模拟触摸操作
element = driver.find_element(By.ID, "target")
actions = ActionChains(driver)

# 触摸点击
actions.click(element).perform()

# 触摸滑动
actions.click_and_hold(element).move_by_offset(0, -100).release().perform()

driver.quit()

实战示例 #

表单填写与提交 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

def fill_form():
    driver = webdriver.Chrome()
    driver.get("https://example.com/form")
    
    try:
        # 填写表单
        driver.find_element(By.ID, "name").send_keys("张三")
        driver.find_element(By.ID, "email").send_keys("zhangsan@example.com")
        driver.find_element(By.ID, "phone").send_keys("13800138000")
        
        # 选择下拉框
        from selenium.webdriver.support.ui import Select
        Select(driver.find_element(By.ID, "city")).select_by_visible_text("北京")
        
        # 勾选复选框
        driver.find_element(By.ID, "agree").click()
        
        # 使用 Tab 键导航并提交
        actions = ActionChains(driver)
        actions.send_keys(Keys.TAB).send_keys(Keys.ENTER).perform()
        
        print("表单提交成功")
        
    finally:
        driver.quit()

fill_form()

拖拽排序 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

def drag_sort():
    driver = webdriver.Chrome()
    driver.get("https://example.com/sortable")
    
    try:
        # 获取所有可排序项
        items = driver.find_elements(By.CSS_SELECTOR, ".sortable-item")
        
        actions = ActionChains(driver)
        
        # 将第一项拖到最后一项的位置
        actions.drag_and_drop(items[0], items[-1]).perform()
        
        # 或者使用偏移量拖拽
        actions.click_and_hold(items[1]).move_by_offset(0, 100).release().perform()
        
        print("拖拽排序完成")
        
    finally:
        driver.quit()

drag_sort()

悬停菜单操作 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def hover_menu():
    driver = webdriver.Chrome()
    driver.get("https://example.com")
    
    try:
        actions = ActionChains(driver)
        
        # 悬停到主菜单
        main_menu = driver.find_element(By.CSS_SELECTOR, ".nav-menu")
        actions.move_to_element(main_menu).perform()
        
        # 等待子菜单出现
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, ".submenu"))
        )
        
        # 悬停到子菜单项
        submenu_item = driver.find_element(By.CSS_SELECTOR, ".submenu-item")
        actions.move_to_element(submenu_item).click().perform()
        
        print("菜单操作完成")
        
    finally:
        driver.quit()

hover_menu()

文本选择与复制 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

def select_and_copy():
    driver = webdriver.Chrome()
    driver.get("https://example.com")
    
    try:
        textarea = driver.find_element(By.ID, "content")
        textarea.send_keys("这是一段需要选择的文本内容")
        
        actions = ActionChains(driver)
        
        # 方式一:全选
        actions.click(textarea).key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform()
        
        # 复制
        actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
        
        # 移动到另一个输入框并粘贴
        target = driver.find_element(By.ID, "target")
        actions.click(target).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
        
        print("文本复制完成")
        
    finally:
        driver.quit()

select_and_copy()

滑块验证 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

def slider_verification():
    driver = webdriver.Chrome()
    driver.get("https://example.com/slider")
    
    try:
        slider = driver.find_element(By.ID, "slider-button")
        
        actions = ActionChains(driver)
        
        # 点击并按住滑块
        actions.click_and_hold(slider)
        
        # 移动滑块到目标位置
        actions.move_by_offset(200, 0)
        
        # 释放滑块
        actions.release().perform()
        
        print("滑块验证完成")
        
    finally:
        driver.quit()

slider_verification()

最佳实践 #

操作稳定性 #

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def stable_interaction():
    driver = webdriver.Chrome()
    
    try:
        driver.get("https://example.com")
        
        # 等待元素可点击
        element = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.ID, "button"))
        )
        
        # 滚动到元素可见
        driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", element)
        
        # 添加短暂暂停确保操作稳定
        actions = ActionChains(driver)
        actions.move_to_element(element).pause(0.5).click().perform()
        
    finally:
        driver.quit()

跨平台兼容 #

python
import platform
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

def get_modifier_key():
    """根据操作系统返回修饰键"""
    if platform.system() == 'Darwin':  # macOS
        return Keys.COMMAND
    else:  # Windows/Linux
        return Keys.CONTROL

def cross_platform_copy_paste():
    driver = webdriver.Chrome()
    
    try:
        driver.get("https://example.com")
        
        modifier = get_modifier_key()
        input_field = driver.find_element(By.ID, "input")
        
        actions = ActionChains(driver)
        
        # 全选
        actions.key_down(modifier).send_keys('a').key_up(modifier).perform()
        
        # 复制
        actions.key_down(modifier).send_keys('c').key_up(modifier).perform()
        
        # 粘贴
        actions.key_down(modifier).send_keys('v').key_up(modifier).perform()
        
    finally:
        driver.quit()

下一步 #

掌握了用户交互后,接下来学习 等待机制 了解如何处理异步加载和动态元素!

最后更新:2026-03-28