PythonとAppiumでswipe()で画面をスワイプする方法

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.quite()メソッドでAppium接続を終了します。

bannerAds