pip install appium-python-client
from appium import webdriver
# 初始化配置,设置Desired Capabilities参数
desired = {
"platformName": "Android",
"appPackage": "com.jingdong.app.mall",
"appActivity": "com.jingdong.app.mall.MainFrameActivity",
"platformVersion": "5.1.1",
"deviceName": "OPPO R11 Plus"
}
# 指定Appium Server
server = 'http://localhost:4723/wd/hub'
# 新建一个driver
driver = webdriver.Remote(server, desired)
# 获取模拟器/手机的分辨率(px)
width = driver.get_window_size()['width']
height = driver.get_window_size()['height']
print(width, height)
移动设备分辨率
driver.get_window_size()['width']
driver.get_window_size()['height']
通过APPium获取xpath, 根据selenium用法开发即可
find_element_by_id
find_elements_by_id
find_element_by_xpath
find_elements_by_xpath
element.text
Appium-Python-Client · PyPI
注意:程序运行是先把Appium和模拟器运行
from appium import webdriver # 没有该模块的请 安装 pip install appium-python-client
server = 'http://localhost:4723/wd/hub' # 端口号 可看下图
desired ={
"platformName": "Android",
"platformVersion": "5.1.1",
"deviceName": "OPPO R11 Plus",
"appPackage": "com.android.browser",
"appActivity": "com.android.browser.BrowserActivity"
}
driver = webdriver.Remote(server,desired)
注意:webdriver.Remote(server,desired) 这两个参数是第二步第三步里面的
运行Appium
把xpath复制到我们写的代码里面
#输入内容
input_xpath ='/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[3]/android.view.View/android.widget.EditText'
# 定位元素
input = driver.find_element_by_xpath(input_xpath)
# 在定位的元素里 写上内容
input.send_keys('音乐')
# 点按钮
button_xpath ='/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[3]/android.view.View/android.widget.Button'
button = driver.find_element_by_xpath(button_xpath)
button.click()
# 注意 程序操作appium的时候把appium关掉 避免冲突
可能遇到的错误python操作Appium的时候出现的错误selenium.common.exceptions.NoSuchElementException: Message: An element could n_宠乖仪的博客-CSDN博客
from appium import webdriver
from time import sleep
server = 'http://localhost:4723/wd/hub'
desired ={
"platformName": "Android",
"platformVersion": "5.1.1",
"deviceName": "OPPO R11 Plus",
"appPackage": "com.android.browser",
"appActivity": "com.android.browser.BrowserActivity"
}
# 创建模拟器
driver = webdriver.Remote(server,desired)
tmp_xpath = '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView'
tmp = driver.find_element_by_xpath(tmp_xpath)
#输入内容
input_xpath ='/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[3]/android.view.View/android.widget.EditText'
# 定位元素
input = driver.find_element_by_xpath(input_xpath)
# 在定位的元素里 写上内容
input.send_keys('音乐')
sleep(1)
# 点按钮
button_xpath ='/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[2]/android.view.View[3]/android.view.View/android.widget.Button'
button = driver.find_element_by_xpath(button_xpath)
sleep(1)
button.click()
# 注意 程序操作appium的时候把appium关掉 避免冲突