环境:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. appium解决无法通过name属性识别元素org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session

    执行代码.: public AndroidDriver<AndroidElement> appiumDriver; appiumDriver.findElement(By.name(&qu ...

  5. Appium 运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for (转)

    现象:Appium运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported f ...

  6. Appium移动端自动化:Appium-Desktp的使用以及定位元素方式总结

    一.appium-desktop功能介绍 1.打开appium-desktop,点击start session 2.打开后,点击屏幕右上角的搜索按钮 3.然后会打开配置页面,在本地服务配置信息同上面写 ...

  7. 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 ...

  8. appium新版本不支持findElementByName,切换到findElementByAndroidUIAutomator

    appium 1.7.6 不支持findElementByName(locator)  不知道为什么? 脚本中许多这样的语句,麻烦事情多了 org.openqa.selenium.InvalidSel ...

  9. Appium新版本不再支持ByName定位了怎么办

    appium版本在1.5以后就不再支持ByName的定位,本文章仅介绍在appium1.6.3/1.6.4/1.6.5版本下如何支持ByName定位,适用于安卓.在使用appium1.5之后的版本时, ...

随机推荐

  1. 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path

    The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and s ...

  2. 理解web服务器和数据库的负载均衡以及反向代理

    这里的“负载均衡”是指在网站建设中应该考虑的“负载均衡”.假设我们要搭建一个网站:aaa.me,我们使用的web服务器每秒能处理100条请求,而aaa.me这个网站最火的时候也只是每秒99条请求,那么 ...

  3. 【CSS】305- [译] Web 使用 CSS Shapes 的艺术设计

    %; %; %; %; 0, 0 100%, 100% 100%); %; %; % 0, 0 100%, 100% 100%); %; %; ) p:nth-of-type(1)::before { ...

  4. 浅析Java常量池

    Java常量池 Java常量池其实分为两种:静态常量池和运行时常量池 1.静态常量池 所谓静态常量池,即*.class文件中的常量池,class文件中的常量池不仅仅包含字符串(数字)字面量,还包含类. ...

  5. 【Zuul】使用学习

    [Zuul]使用学习 添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <arti ...

  6. 【CentOS7】修改yum源

    [CentOS7]修改yum源 转载:https://www.cnblogs.com/yangchongxing/p/10645944.html 1.备份源 # mv /etc/yum.repos.d ...

  7. UWP 使用SSL证书,保证数据安全

    事情是这样的,我们后端的小伙伴升级了用户会员系统,使用了全新的GraphQL登录机制,并且采用SSL加密的方式来实现阻止陌生客户端请求的案例. GraphQL在UWP端的实现,以后有时间会单独写一篇文 ...

  8. (Concurrent)HashMap的存储过程及原理。

    1.前言 看完咕泡Jack前辈的有关hashMap的视频(非宣传,jack自带1.5倍嘴速,高效),收益良多,所以记录一下学习到的东西. 2.基础用法 源码的注释首先就介绍了哈希表是基于Map接口,所 ...

  9. 京东云携手HashiCorp,宣布推出Terraform Provider

    2019年4月23日消息,京东云携手云基础设施自动化软件的领导者HashiCorp,宣布推出Terraform Provider for JD Cloud,这意味着用户能够在京东云上轻松使用简单模板语 ...

  10. c++-构造函数练习和delete,new

    强化练习 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostr ...