首页 > 编程语言 >python+uiautomator2安卓,混合app 操作h5页面

python+uiautomator2安卓,混合app 操作h5页面

时间:2023-02-13 18:00:38浏览次数:62  
标签:webdriver python app driver options print uiautomator2 self

参考文章:https://testerhome.com/topics/15998

uiautomator2 app 原生 + webview 的实际操作记录 (安卓)

1. 首先 chrome://inspect/#devices 通过连接查看自己的 版本号 ,

必须和我们的手机应用使用的 WebView 版本相对应

https://npm.taobao.org/mirrors/chromedriver/2.28/notes.txt

下载地址 http://npm.taobao.org/mirrors/chromedriver/

2. 下载对应版本的 放到python安装包下

 

 

 3. 安装atx

pip3 install atx

内部存在 (这里说的 chromedriver.py 是 atx 模块下的,需要安装 atx)

打开 chromedriver.py文件  此处如果有框架封装的话,可以直接修改此文件,后期直接调用即可

 def driver(self,package=None, attach=True, activity=None, process=None):
        """
        Args:
            - package(string): default current running app
            - attach(bool): default true, Attach to an already-running app instead of launching the app with a clear data directory
            - activity(string): Name of the Activity hosting the WebView.
            - process(string): Process name of the Activity hosting the WebView (as given by ps). 
                If not given, the process name is assumed to be the same as androidPackage.

        Returns:
            selenium driver
        """
        app = self._d.current_app()
        print(app)
        options = webdriver.ChromeOptions()
        options.add_experimental_option('androidDeviceSerial', self._d.serial)
        options.add_experimental_option('androidPackage', app.get('package'))
        options.add_experimental_option('androidUseRunningApp', True)
        options.add_experimental_option('androidActivity', app.get('activity'))

        driver = webdriver.Chrome('chromedriver', options=options)

        # if device_ip!=None:
        #     subprocess.call(['adb','tcpip','5555'])
        #     subprocess.Popen(['adb','connect',str(device_ip)])
        # print(app)
        # capabilities = {
        #     'chromeOptions': {
        #         'androidDeviceSerial': device_ip or self._d.serial,
        #         'androidPackage': package or app.get('package'),
        #         'androidUseRunningApp': attach,
        #         'androidProcess': process or app.get('package'),
        #         'androidActivity': activity or app.get('activity'),
        #     }
        # }

        # try:
        #     dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)
        # except URLError:
        #     self._launch_webdriver()
        #     dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)

        # self._launch_webdriver()
        # dr = webdriver.Remote('http://localhost:%d' % self._port, capabilities)
        #
        # always quit driver when done
        atexit.register(driver.quit)
        return driver

    def windows_kill(self):
        subprocess.call(['taskkill', '/F', '/IM', 'chromedriver.exe', '/T'])

然后封装 从原生切入h5的方法:

 def check_into_h5(self):

        drivers = ChromeDriver(self.d).driver()

        return drivers

在用例中直接调用

driver = action.check_into_h5()
print(driver.current_url)
print(driver.page_source)
driver.find_element(By.XPATH, '//span[contains(text(),"爱好")]').click() 

4、定位元素

(1)可以通过  特定的方法进行定位,可自行百度,定位h5元素的方法

(2)此处通过 打印整个页面的元素进行自主定位

# print(driver.current_url)

# print(driver.page_source)

打印对应的driver.page_source ,直接对于元素进行定位,常用定位方法://div[contains(text(),"文本")]

5.单独使用代码如下   

d = u2.connect()
app = d.app_current()
print(app)

options = webdriver.ChromeOptions()
options.add_experimental_option('androidDeviceSerial', d.serial)
options.add_experimental_option('androidPackage', app.get('package'))
options.add_experimental_option('androidUseRunningApp', True)

driver = webdriver.Chrome('chromedriver', options=options)
print(driver.current_url)
driver.find_element(By.XPATH, '//span[contains(text(),"爱好")]').click()

 

标签:webdriver,python,app,driver,options,print,uiautomator2,self
From: https://www.cnblogs.com/zz-1021/p/17110109.html

相关文章

  • 前端APP版本号对比判断是否需要更新
    functioncompareVersion(newVersion,currentVersion){constv1=newVersion.split('.');constv2=currentVersion.split('.');......
  • 数据存储单位、编程语言的发展史、python解释器版本、解释器的下载与安装、多版本共存
    目录一、数据存储单位(1)、简介(2)、换算单位二、编程语言的发展史(1)、机器语言(2)、汇编语言(3)、高级语言三、编程语言的分类(1)、编译型语言(2)、解释型语言四、python......
  • python 特殊方法
    ##跟运算符无关的特殊方法###1.实例的创建和销毁__init__解释:构造方法,可调用超类的构造器。这是实例级别的方法,发生在实例创建之后,用于初始化实例,给实例添加一些属性和......
  • ios打开第三方地图app
    https://www.jianshu.com/p/691dd39cb28c 【ios调起地图app】分三步:1、配置相对于地图app的LSApplicationQueriesSchemes白名单百度地图:baidumap高德地图:iosamap......
  • Python中如何求1-100的奇数和?方法详解!
    在之前的文章中,老男孩IT教育小编为大家介绍过Python的特点、优势、用途以及薪资待遇等知识,而为了帮助大家更好的掌握Python,小编将为大家讲解一些实战案例,比如:Python中如......
  • springboot 读取配置 application.yml
    添加配配置信息spring:application:name:dataExChangePlatformdevtools:restart:enabled:trueadditional-paths:src/main/javae......
  • MobPush 助力工具 APP 激活用户,留存率提升 31%
    近几年里,吸引用户注意力的争夺在内容资讯行业十分激烈。在此背景下,能吸引用户注意力,能够高效连接内容与用户的APP将发挥出更大的增长潜力。近期,某工具APP与MobTech携......
  • uniapp 小程序引流关注公众号
    official-account组件说明:微信公众号关注组件。当用户扫小程序码打开小程序时,开发者可在小程序内配置公众号关注组件,方便用户快捷关注公众号,可嵌套在原生组件内。仅微信......
  • 转载:python subprocess.Popen系列问题
    pythonsubprocess.Popen系列问题_水军总督的博客-CSDN博客_pythonsubprocess.popen返回的pid跟ps看到的不一样 python中subprocess.Popen总结_唐僧洗头用飘柔dp的......
  • OpenCV-Python扩充数据集
    在深度学习中,数据集规模直接影响模型训练后的性能。当我们数据集较少的时候可以通过不同的手段对现有的数据进行扩充。比如裁切,翻转,旋转,加入噪点,调整亮度等手段进行数据集......