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

如何在Android设备上运行Python Selenium脚本?

Hey there! Let's break down how to adapt your Windows-based Python Selenium script to run on Android devices—you're right that Selendroid and Appium aren't just for connecting to devices, they're exactly the tools you need for actual script porting. Selendroid is pretty outdated these days, so we'll focus on Appium 2.x, which is the modern standard for mobile web automation. Here's a step-by-step guide to make this work:

1. First, Clarify the Core Shift

Your original Selenium script controls desktop browsers (like Chrome on Windows). On Android, you'll be automating the Chrome browser installed on your Android device (or another mobile browser like Firefox). Appium acts as a bridge between your Python code and the Android device, translating Selenium-style commands into actions the mobile browser understands.

2. Set Up Your Environment

Get these tools in place first:

  • Install Appium 2.x: Run npm install -g appium in your terminal (you'll need Node.js installed first). Then add the Android driver with appium driver install uiautomator2—this is the driver that lets Appium communicate with Android devices.
  • Prepare your Android device: Enable USB debugging in the developer options, connect it to your computer via USB, and verify the connection with adb devices (you'll need Android SDK tools installed for ADB).
  • Install the Python Appium client: Run pip install Appium-Python-Client to get the library that lets your Python code talk to Appium.
3. Adapt Your Selenium Code to Appium

The good news is most of your existing Selenium logic (like element定位, clicks, text input) will stay the same—only the initialization part changes. Here's a side-by-side example:

Original Windows Selenium Code:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://example.com")
# Your existing element interactions here

Modified Appium Code for Android:

from appium import webdriver

# Define "desired capabilities" to tell Appium which device/browser to target
desired_caps = {
    "platformName": "Android",
    "deviceName": "YourDeviceID",  # Grab this from the output of `adb devices`
    "browserName": "Chrome",
    "automationName": "UiAutomator2",
    # Optional: Add this if you run into W3C compatibility issues with older Chrome versions
    "chromeOptions": {"w3c": False}
}

# Connect to the running Appium server (default port is 4723)
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
driver.get("https://example.com")

# Your existing element interactions work here too!
# For example: driver.find_element(By.ID, "some-element-id").click()
4. Handle Mobile-Specific Tweaks
  • Element adjustments: Mobile web pages are responsive, so some selectors from your desktop script might not work. Use Chrome's remote inspection to fix this: On your Android Chrome, go to chrome://inspect, then open chrome://inspect on your desktop Chrome—you'll see the mobile page and can inspect elements just like on desktop.
  • Scrolling: If you need to scroll the page, use Appium's built-in methods. For example:
    # Swipe up (adjust coordinates based on your device's screen size)
    driver.swipe(start_x=500, start_y=1500, end_x=500, end_y=500, duration=800)
    
  • Popups: Android might throw permission prompts or Chrome might show notification requests. You'll need to add code to handle these, e.g., locating the "Allow" button and clicking it.
5. Test and Troubleshoot
  • Start the Appium server first: Run appium in your terminal before executing your script.
  • If you get connection errors: Double-check that ADB recognizes your device (adb devices), and that your desired capabilities have the correct deviceName (use the ID from adb devices).
  • Chrome version mismatches: If Appium can't find the right ChromeDriver, specify the version in desired_caps with "chromeDriverVersion": "x.y.z" (match it to the Chrome version on your Android device).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:01:36