环境:

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. C和C++从零开始系列(二)

    今天说一下 C和C++ 的if 条件语句. 在实际编程中,会经常有逻辑判断,比如,输入的数值参数中,如果是奇数,输出This is uneven. 如果是偶数,输出 This is even. 我们在 ...

  2. BGA256芯片植球全过程体验(原创)

    今天工具到位,迫不亟待,需要对手上的BGA256的FPGA芯片进行植球, 该芯片买来的时候是有球的,只是在焊接后,由于电路板故障或焊接问题,需要拆下来芯片,导致球损失,需要重新植球. 一般植球都是将所 ...

  3. 【Web技术】400- 浅谈Shadow DOM

    编者按:本文作者:刘观宇,360 奇舞团高级前端工程师.技术经理,W3C CSS 工作组成员. 为什么会有Shadow DOM 你在实际的开发中很可能遇到过这样的需求:实现一个可以拖拽的滑块,以实现范 ...

  4. 改变SecureCRT的背景颜色

    1.在使用secureCRT客户端时,可以连接服务器,默认为白色底. 2.要进行对把底色的白色改为黑色的底色,右击的窗口的位置. 3.下拉菜单中点击 Session Options 4.点击Appea ...

  5. java发送邮件基础方法(另附部分主流邮箱服务器地址、端口及设置方法)

    java发送邮件基础方法,可通过重载简化参数 import java.io.File; import java.io.UnsupportedEncodingException; import java ...

  6. python笔记03

    day03 一.今日内容: 1.整型 2.字符串 3.布尔类型 二.内容回顾和补充 脑图--xmind软件,processon 1.运算符补充(in.not in) value = "我是中 ...

  7. 【集合系列】- 深入浅出分析 ArrayDeque

    一.摘要 在 jdk1.5 中,新增了 Queue 接口,代表一种队列集合的实现,咱们继续来聊聊 java 集合体系中的 Queue 接口. Queue 接口是由大名鼎鼎的 Doug Lea 创建,中 ...

  8. 【同步工具类】CountDownLatch闭锁任务同步

    [同步工具类]CountDownLatch闭锁任务同步 转载:https://www.cnblogs.com/yangchongxing/p/9214284.html 打过dota的同学都知道,多人一 ...

  9. C++构造函数的几种使用方法

    在C++中,有一种特殊的成员函数,他的名字和类相同,没有返回值,不需要用户显示调用,用户也无法调用,而是在创建对象的时候自动执行. 这种特殊的函数就是构造函数 Constructor 构造函数的名字与 ...

  10. webpack安装出错(电脑设置了代理)

    安装webpack的时候发现第一句话就报错了,之后查了一下找到原因,原来是因为设置了代理服务原文 ,参考了之后知道是因为代理问题,就按着来做