这是因为Appium 1.21.0版本默认使用Electron 13,但该版本的Electron已经没有可用的Linux版本。可以通过以下两种方法解决:
1.升级Node.js到v14或更高版本(建议使用nvm管理多个Node.js版本),这样就可以使用Electron v15或更高版本,可以解决找不到模块“electron”的问题。
2.对于不想或无法升级Node.js的用户,可以在Appium启动代码中手动指定使用Electron的版本(如v12)。可以通过以下代码实现:
from appium import webdriver from appium.webdriver.appium_service import AppiumServiceBuilder
service = AppiumServiceBuilder().using_driver_executable('/usr/local/bin/appium').using_port(4723).with_arguments('--allow-insecure','--relaxed-security','--disable-dev-shm-usage').use_any_free_port().with_environment('ENV_VAR1=foo', 'ENV_VAR2=bar').with_appium_js('/usr/local/lib/node_modules/appium/build/lib/main.js').with_node_options(['-r', 'ts-node/register']).with_capabilities({ "platformName": "Android", "deviceName": "Pixel 2", "app": "/path/to/my/app.apk", "automationName": "UiAutomator2", "appium:chromeOptions": {"w3c": False}, "appium:electronChromedriverExecutable": "/path/to/electron/chromedriver-v12.0.0", "appium:electronChromeArgs": ["remote-debugging-port=9222"] }).build()
driver = webdriver.Remote(service.service_url, desired_capabilities={}) driver.find_element_by_id("com.example.android.myApp:id/button").click()
注意,在with_capabilities方法中添加以下两个参数:
"appium:electronChromedriverExecutable": "/path/to/electron/chromedriver-v12.0.0" "appium:electronChromeArgs": ["remote-debugging-port=9222"]
这些参数指定使用Electron的版本(v12)和要使用的Chromedriver