参考文章: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