共計 1203 個字符,預計需要花費 4 分鐘才能閱讀完成。
在 Python 中使用 Appium 進行滑動屏幕操作,可以通過以下代碼實現:
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
# Appium 連接配置
desired_caps = {'platformName': 'Android',
'platformVersion': '10',
'deviceName': 'Android Emulator',
'appPackage': 'your_app_package',
'appActivity': 'your_app_activity',
'automationName': 'UiAutomator2'
}
# Appium 服務器連接地址
appium_url = 'http://localhost:4723/wd/hub'
# 連接 Appium 服務器
driver = webdriver.Remote(appium_url, desired_caps)
# 獲取屏幕寬度和高度
screen_width = driver.get_window_size()['width']
screen_height = driver.get_window_size()['height']
# 設置起始點和終點坐標
start_x = int(screen_width * 0.5)
start_y = int(screen_height * 0.8)
end_x = int(screen_width * 0.5)
end_y = int(screen_height * 0.2)
# 執行滑動操作
TouchAction(driver).press(x=start_x, y=start_y).wait(1000).move_to(x=end_x, y=end_y).release().perform()
# 關閉 Appium 連接
driver.quit()
在以上代碼中,首先需要配置 Appium 連接和啟動參數,然后通過 webdriver.Remote()
方法連接 Appium 服務器。接著使用 driver.get_window_size()
方法獲取屏幕寬度和高度,根據需要設置起始點和終點坐標。最后使用 TouchAction
類的方法執行滑動操作,并通過 press()
、wait()
、move_to()
和release()
方法設置滑動動作的起點、持續時間和終點位置。完成滑動操作后,使用 driver.quit()
方法關閉 Appium 連接。
丸趣 TV 網 – 提供最優質的資源集合!
正文完