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

如何在安卓端使用Pydroid、Selendroid运行Selenium Python脚本

问题原因说明

你遇到的安装报错是因为新版Selenium依赖的部分加密库需要Rust编译环境,Pydroid默认没有提供完整的原生编译链,无法完成依赖编译,安装Rust的脚本默认适配的是桌面端Linux环境,和安卓架构不兼容,所以也会报错。

适配Selendroid的完整解决步骤

1. 安装兼容版本Selenium

直接安装不需要Rust编译的低版本Selenium,在Pydroid终端执行以下命令:
pip install selenium==3.141.0 urllib3==1.26.15

2. 配置Selendroid运行环境

  • 进入安卓系统设置,开启「开发者选项」,打开「USB调试」权限
  • 安装Selendroid服务端到你的安卓设备,启动服务后默认会监听本地4723端口
  • 提前在安卓设备上安装Chrome浏览器,确保可以正常访问网页

3. 修改原有脚本适配移动端环境

原有脚本是桌面端Windows环境的配置,需要替换驱动连接逻辑适配Selendroid,修改后的脚本如下:

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

# Selendroid运行参数配置,按你的实际设备修改参数
desired_caps = {
    "platformName": "Android",
    "platformVersion": "你的安卓系统版本,例如12",
    "deviceName": "任意设备标识,例如android_test",
    "browserName": "Chrome",
    "automationName": "Selendroid",
    "noReset": True
}

# 连接本地Selendroid服务
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_capabilities=desired_caps)
driver.get("https://www.amazon.it/dp/B08T769JQD")

title = driver.find_element(By.ID, "productTitle").text
print(title)
# 执行完毕后关闭驱动释放资源
driver.quit()
更简单的无Selendroid替代方案

如果你不想额外配置Selendroid,也可以直接调用安卓本机Chrome的调试能力运行脚本,不需要额外安装服务:

  1. 打开安卓Chrome浏览器,地址栏输入chrome://flags,开启「Android webview debugging」选项,保持Chrome后台运行
  2. 使用如下脚本直接运行:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 指定调用安卓本地Chrome
chrome_options.add_experimental_option("androidPackage", "com.android.chrome")
# 可添加无界面参数
chrome_options.add_argument("--headless=new")
chrome_options.add_argument('--log-level=1')

driver = webdriver.Chrome(options=chrome_options)
driver.get('https://www.amazon.it/dp/B08T769JQD')

title = driver.find_element(By.ID, "productTitle").text
print(title)
driver.quit()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 15:42:03