如何在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:
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.
Get these tools in place first:
- Install Appium 2.x: Run
npm install -g appiumin your terminal (you'll need Node.js installed first). Then add the Android driver withappium 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-Clientto get the library that lets your Python code talk 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()
- 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 openchrome://inspecton 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.
- Start the Appium server first: Run
appiumin 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 correctdeviceName(use the ID fromadb devices). - Chrome version mismatches: If Appium can't find the right ChromeDriver, specify the version in
desired_capswith"chromeDriverVersion": "x.y.z"(match it to the Chrome version on your Android device).
内容的提问来源于stack exchange,提问作者KOHTPOJIEP

