环境:

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. E - Unimodal Array CodeForces - 831A

    Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is cons ...

  2. 使用Spring Boot和AspectJ实现方法跟踪基础结构

    了解如何使用Spring Boot和AspectJ实现方法跟踪基础结构!最近在优锐课学习收获颇多,记录下来大家一起进步! 在我们的应用程序中,获取方法的堆栈跟踪信息可能会节省很多时间.具有输入输出参数 ...

  3. Day 05 文本处理和爬虫基础1

    目录 什么是文件 什么是文本 如何通过文本编辑器控制.txt文件 打开文件的三种模式 t和b模式 高级应用 文本处理 + 词云分析 效果如下 爬虫原理 requests模块 re模块 爬取图片 爬取视 ...

  4. 【Seleniuem】selenium.common.exceptions.InvalidSelectorException

    selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illega ...

  5. 【Eclipse】安装配置

    [Eclipse]安装配置 官网:https://www.eclipse.org 全部版本下载:https://www.eclipse.org/downloads/packages/

  6. CookieUtils-浏览器缓存工具类

    package cn.yonyong.myproject.commons.utils; import javax.servlet.http.Cookie; import javax.servlet.h ...

  7. Table实现表头固定 内容滚动

    <div style="width: 800px;"> <div class="table-head"> <table> & ...

  8. spring boot 2 + shiro 实现简单的身份验证例子

    Shiro是一个功能强大且易于使用的Java安全框架,官网:https://shiro.apache.org/. 主要功能有身份验证.授权.加密和会话管理.其它特性有Web支持.缓存.测试支持.允许一 ...

  9. oop面向对象【类与对象、封装、构造方法】

    今日内容 1.面向对象 2.类与对象 3.三大特征——封装 4.构造方法 教学目标 1.能够理解面向对象的思想 2.能够明确类与对象关系 3.能够掌握类的定义格式 4.能够掌握创建对象格式,并访问类中 ...

  10. SVN清理失败(clean up)或者(lock)问题进入死循环最终解决方案

    解决方法: step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe step2: 将下载到的 sqlite3.ex ...