python 爬虫 移动端开发(四)

一、 appium-python-client使用

1.1 安装appium-python-client模块

pip install appium-python-client

1.2 初始化以及获取移动设备分辨率

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']

1.3 获取标签

通过APPium获取xpath, 根据selenium用法开发即可

find_element_by_id
find_elements_by_id
find_element_by_xpath
find_elements_by_xpath

1.4 获取内容

element.text

1.5 appium 文档

Appium-Python-Client · PyPI

1.6 实战(python操作Appium)

注意:程序运行是先把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博客

 1.7 完整代码

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关掉 避免冲突

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宠乖仪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值