栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 爬虫 移动端开发(四)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/338998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号