You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Chrome移动端模拟触摸功能失效,如何启用该功能?

问题描述

用Selenium模拟Chrome移动端时,我配置了如下设备参数来启用触摸功能:

"deviceMetrics" => { "width" => 360, "height" => 640, "pixelRatio" => 3.0, touch: true }

代码能正常运行,但触摸功能完全失效——没法进行触摸、滑动操作,也触发不了任何触摸事件。感觉原来的触摸配置方式可能被弃用了,求正确启用触摸功能的方法。

我用Ruby和Python编写代码,Python环境信息:

  • Python 3.7.7
  • selenium 4.11.2
  • selenium-wire 5.1.0
  • Chrome 116.0.5845.187
  • ChromeDriver 116.0.5845.96

Python代码示例:

mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0, "touch": True, "mobile": True },
    "userAgent": "Mozilla/5.0 (Linux; Android 11; SM-G977U Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36",
    "clientHints": {"platform": "Android", "mobile": True} }

chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_options.add_experimental_option("detach",True)

driver = webdriver.Chrome(chrome_options = chrome_options)
解决办法

1. 直接用Chrome预设的移动设备名称

Chrome内置了大量成熟的移动设备配置,直接指定设备名就能自动启用触摸功能,比手动配置参数更可靠:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 这里以Galaxy S20为例,也可替换为"iPhone 13"等其他预设设备名
chrome_options.add_experimental_option("mobileEmulation", {"deviceName": "Galaxy S20"})
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)

2. 用CDP命令强制开启触摸模拟

如果必须手动配置设备参数,新版本Chrome仅在deviceMetrics里设置touch: true已经不生效,需要额外通过Chrome DevTools协议命令开启触摸:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (Linux; Android 11; SM-G977U Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36",
    "clientHints": {"platform": "Android", "mobile": True}
}

chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)
# 执行CDP命令启用触摸,maxTouchPoints设为5模拟真实手机的多点触摸
driver.execute_cdp_cmd("Emulation.setTouchEmulationEnabled", {"enabled": True, "maxTouchPoints": 5})

3. 排查selenium-wire的影响

你使用了selenium-wire代理工具,它有可能干扰Chrome的触摸配置。可以先移除selenium-wire,用原生webdriver测试,确认是否是代理导致的触摸功能失效。

Ruby版本对应的解决代码

如果是用Ruby编写的代码,同样可以通过CDP命令开启触摸:

require 'selenium-webdriver'

mobile_emulation = {
  "deviceMetrics" => { "width" => 360, "height" => 640, "pixelRatio" => 3.0 },
  "userAgent" => "Mozilla/5.0 (Linux; Android 11; SM-G977U Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36",
  "clientHints" => {"platform" => "Android", "mobile" => true}
}

options = Selenium::WebDriver::Chrome::Options.new
options.add_experimental_option("mobileEmulation", mobile_emulation)
options.add_experimental_option("detach", true)

driver = Selenium::WebDriver.for :chrome, options: options
# 启用触摸模拟
driver.execute_cdp('Emulation.setTouchEmulationEnabled', enabled: true, maxTouchPoints: 5)

内容的提问来源于stack exchange,提问作者GokuSleepy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:30:59