Appium新版本遇到的问题,不能通过 name 去定位元素抛 Message: Locator Strategy 'name' is not supported for this session
环境:
1、Appium: 1.15.1
2、Python: 3.7.0
3、Selenium: 3.141.0
4、IDE: Pycharm
5、PC:Windows 10
问题:在 Pycharm 中输入 driver.find_element_by_后可以直接联想出name这个,然后就通过这个name属性去定位界面元素,在运行时居然报 "Locator Strategy 'name' is not supported for this session",从这个错误来看,显然可以看出不是selenium的锅,要不然 pycharm 也不可能联想出来,那么这个很可能是 appium 不支持这个属性了,据说是从appium 1.5版本之后就不支持这个了,为了验证这个观点,到这个目录下:C:\Program Files\Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\build\lib 找到 driver.js 打开如下,看到没它只支持下面五种属性
- class AndroidDriver extends _appiumBaseDriver.BaseDriver {
- constructor(opts = {}, shouldValidateCaps = true) {
- super(opts, shouldValidateCaps);
- this.locatorStrategies = ['xpath', 'id', 'class name', 'accessibility id', '-android uiautomator'];
- this.desiredCapConstraints = _desiredCaps.default;
- this.sessionChromedrivers = {};
- this.jwpProxyActive = false;
- this.jwpProxyAvoid = _lodash.default.clone(NO_PROXY);
- this.settings = new _appiumBaseDriver.DeviceSettings({
那么如果一定要用name这个属性来进行元素定位的话,那么如何操作呢,下面介绍两种方法:
1、修改driver.js文件,添加name到locatorStrategies里去,经验证这种方法不可行,仍然报同样错误,显然通过简单的修改这个方式不太可行
- class AndroidDriver extends _appiumBaseDriver.BaseDriver {
- constructor(opts = {}, shouldValidateCaps = true) {
- super(opts, shouldValidateCaps);
- this.locatorStrategies = ['xpath', 'id', 'class name', 'accessibility id', '-android uiautomator', 'name'];
- this.desiredCapConstraints = _desiredCaps.default;
- this.sessionChromedrivers = {};
- this.jwpProxyActive = false;
- this.jwpProxyAvoid = _lodash.default.clone(NO_PROXY);
- this.settings = new _appiumBaseDriver.DeviceSettings({
- driver.find_element_by_name("Browsing").click()
2、通过find_element_by_android_uiautomator这个来进行元素定位,这个是基于uiautomator原生的来支持,这个肯定是会支持界面中所有支持的元素定位,直接上脚本,这个是可以正常跑通的。
- # -*- coding:utf-8 -*-
- import unittest
- import time
- from appium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- server = 'http://localhost:4723/wd/hub'
- desired_capabilities = {
- 'platformName': 'Android',
- 'deviceName': 'abcdefg1234',
- 'appPackage': 'com.sina.weibo',
- 'appActivity': 'com.sina.weibo.VisitorMainTabActivity',
- 'autoGrantPermissions': True
- }
- driver = webdriver.Remote(server, desired_capabilities)
- time.sleep(30)
- driver.find_element_by_android_uiautomator("new UiSelector().text(\"Browsing\")").click()
建议:以后在写适用appium自动化框架的脚本时,凡是不能通过这几个属性['xpath', 'id', 'class name', 'accessibility id']直接定位元素的,都直接用 '-android uiautomator' 这个属性来进行定位,其实大家也能发现,前面那几个属性在uiautomator里面是全部包括的(xpath除外),下面把这几个对应关系列举如下:
driver.find_element_by_id("com.sina.weibo:id/tv_title_lookaround").click() <==> driver.find_element_by_android_uiautomator("new UiSelector().resourceId(\"com.sina.weibo:id/tv_title_lookaround\")").click()
driver.find_element_by_name("Browsing").click() <==> driver.find_element_by_android_uiautomator("new UiSelector().text(\"Browsing\")").click()
driver.find_element_by_accessibility_id("Browsing").click() <==> driver.find_element_by_android_uiautomator("new UiSelector().description(\"Browsing\")").click()
driver.find_element_by_class_name("android.widget.TextView").click() <==> driver.find_element_by_android_uiautomator("new UiSelector().className(\"android.widget.TextView\")").click()
注:以上四个对应关系并没有一个一个去验证,只是根据自己的理解写出来,有错误的地方还请各路英雄好汉指出,谢谢!
Appium新版本遇到的问题,不能通过 name 去定位元素抛 Message: Locator Strategy 'name' is not supported for this session的更多相关文章
- Appium问题解决方案(5)- selenium.common.exceptions.InvalidSelectorException: Message: Locator Strategy 'name' is not supported for this session
背景 使用Appium Server 1.15.1版本 执行了以下脚本 test = driver.find_element_by_name("自动化测试") print(test ...
- appium 使用name 定位报错 Locator Strategy 'name' is not supported for this session【appium-desktop】
RF中使用 name定位 报错提示: Locator Strategy 'name' is not supported for this session 解决: 1.打开本地文件 driver.js ...
- appium 使用name 定位报错 Locator Strategy 'name' is not supported for this session
RF中使用 name定位 报错提示: Locator Strategy 'name' is not supported for this session 解决: 1. 打开本地文件 driver.js ...
- appium解决无法通过name属性识别元素org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session
执行代码.: public AndroidDriver<AndroidElement> appiumDriver; appiumDriver.findElement(By.name(&qu ...
- Appium 运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for (转)
现象:Appium运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported f ...
- Appium移动端自动化:Appium-Desktp的使用以及定位元素方式总结
一.appium-desktop功能介绍 1.打开appium-desktop,点击start session 2.打开后,点击屏幕右上角的搜索按钮 3.然后会打开配置页面,在本地服务配置信息同上面写 ...
- RobotFramework+Appium 升级Appium v1.10.0后,执行click element时报错:InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for this session,解决办法
报错信息如下: debug] [35m[XCUITest][39m Connection to WDA timed out[debug] [35m[XCUITest][39m Connection t ...
- appium新版本不支持findElementByName,切换到findElementByAndroidUIAutomator
appium 1.7.6 不支持findElementByName(locator) 不知道为什么? 脚本中许多这样的语句,麻烦事情多了 org.openqa.selenium.InvalidSel ...
- Appium新版本不再支持ByName定位了怎么办
appium版本在1.5以后就不再支持ByName的定位,本文章仅介绍在appium1.6.3/1.6.4/1.6.5版本下如何支持ByName定位,适用于安卓.在使用appium1.5之后的版本时, ...
随机推荐
- C语言之推箱子游戏代码
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:Yan_Less 正文 新手注意:如果你学习遇到问题找不到人解答,可以点 ...
- 从零开始的openGL——五、光线追踪
前言 前面介绍了基本图形.模型.曲线的绘制,但是,在好像还没有感受到那种3D游戏里一些能惊艳到自己的效果,即真实感还不是很足.这篇文章中介绍的光线追踪,是实现真实感必不可少的.拿下面的两张图片来对比 ...
- 【JS】307- 复习 Object.assign 原理及其实现
点击上方"前端自习课"关注,学习起来~ }let b = { name: "muyiy", book: { title: " ...
- 【Seleniuem】selenium.common.exceptions.InvalidSelectorException
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illega ...
- wxxcx_learn
32个字符组成的一组随即字符串 function getRandChar($length){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWX ...
- Hexo+Github个人博客搭建 | 实战经验分享
概述 第一次尝试搭建属于自己的博客,并且成功了,非常开心. 很久之前就想搭建一个博客,可是也一直没有行动,最近在逛B站的时候发现一个up主(CodeSheep)的一个视频 <手把手教你从0开始搭 ...
- ansible 基础命令
ansible 命令总结 1. Ad-HOC: 适合临时执行任务2. Playbook: 适合一些复杂的部署和配置环境 一 . Ad-HOC: 适合临时执行任务ansible-doc -l 查看ans ...
- 《Java基础知识》Java类的定义及其实例化
类必须先定义才能使用.类是创建对象的模板,创建对象也叫类的实例化. 下面通过一个简单的例子来理解Java中类的定义: public class Dog { String name; int age; ...
- MySql全文检索使用详解
实际项目中经常会有一个字段存储多个值用逗号分隔的场景,当分开查询的时候,使用模糊查询会非常影响效率.mysql提供了全文检索函数可以有效解决这一问题: 1.数据结构 ID CODE MSG 1 111 ...
- C#程序编写高质量代码改善的157个建议【10-12】[创建对象时需要考虑是否实现比较器、区别对待==和Equals]
前言 建议10.创建对象时需要考虑是否实现比较器 建议11.区别对待==和Equals 建议12.重写Equals时也要重写GetHashCode 建议10.创建对象时需要考虑是否实现比较器 有对象的 ...