Appium find_element_by_xpath()方法这样获取元素有什么问题?

Shawzhang 2015-12-30 11:26:47
用Appium框架做自动化测试,使用Python编写测试脚本时,用find_element_by_xpath()方法来定位元素



这个元素的属性:



所以我是这样定位的:

driver.find_element_by_xpath("//android.widget.FrameLayout[0]/android.widget.TabHost[6]/android.widget.TabWidget[0]/android.widget.RelativeLayout[contains(@index,1)]").click()

结果log显示 Could not find an element using supplied strategy.

这样有什么问题,为什么定位不到?
...全文
3253 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵Sir 2016-01-07
  • 打赏
  • 举报
回复
你一层一层的找就知道问题出在哪里了,find_element_by_xpath().find_element_by_xpath()
python_xinshou 2015-12-31
  • 打赏
  • 举报
回复
self.driver.find_element_by_xpath("//android.widget.TabHost/android.widget.TabWidget/android.widget.RelativeLayout[2]") 试试这样行不?
自动化个人学习第一步笔记import os import time import logging import configparser from appium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait base_path=os.path.abspath(os.path.dirname(os.path.dirname(__file__))) class comm: @staticmethod def driver(): """APPIUM驱动""" desired_caps={} desired_caps['platformName']=comm.conf(section='desired_caps',key='platformName') # 手机操作系统 desired_caps['deviceName']=comm.conf(section='desired_caps',key='deviceName') # 手机设备号 desired_caps['platformVersion']=comm.conf(section='desired_caps',key='platformVersion') # 操作系统版本 desired_caps['appPackage']=comm.conf(section='desired_caps',key='appPackage') # app包名 desired_caps['appActivity']=comm.conf(section='desired_caps',key='appActivity') # app ACTIVITY名称 desired_caps['resetKeyboard']=True # 是否在测试结束后将键盘重轩为系统默认的输入法。 desired_caps['newCommandTimeout']=comm.conf(section='desired_caps',key='newCommandTimeout') # Appium服务器待appium客户端发送新消息的时间。默认为60秒 desired_caps['noReset'] = True # true:不重新安装APP,false:重新安装app desired_caps['automationName'] = comm.conf(section='desired_caps',key='automationName') # appium1.5以后的版本才支持toast定位 driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) return driver @staticmethod def get_element(driver,by,selector): """ description:封装定位方法;id指的是resource-id,class_name指的是class,uiautomator使用方法:'new UiSelector().text("文字内容")',xpath定位元素 uthor:clay date: 2020-5-08 params: driver 驱动,by 选择定位的方式,selector 定位的语法 """ if by == 'id': element=driver.find_element_by_id(selector) elif by =='class_name': element=driver.find_element_by_class_name(selector) elif by =='uiautomator': element=driver.find_elemen_by_android_uiautomator(selector) elif by == 'xpath': element=driver.find_element_by_xpath(selector) else: raise NameError return element @staticmethod def tap(driver,upper_left_x,upper_left_y,bottom_right_x,bottom_right_y,time): """ description:根据坐标触发点击事件 uthor:clay date: 2020-5-08 params: driver:驱动,upper_left_x:左上角坐标X轴,upprt_lef_y:左上角Y轴,bottom_right_x:右下角X轴,bottom_right_y:右下角Y轴,time:点击停留的时间 ms为单位 """ tap_=driver.tap([(upper_left_x,upper_left_y),(bottom_right_x,bottom_right_y)],time) return tap_ @staticmethod def tips(driver, text): """ descirption:xpath定位toast提示 author:supper date: 2029-10-06 params: text:元素的text文本 """ try: toast_loc = ("xpath", "//*[contains(@text,'%s')]" % text) toast = WebDriverWait(driver, 5, 0.1).until(EC.presence_of_element_located(toast_loc)) text = toast.text print('提示为:%s' % text) return True except: return False

5,177

社区成员

发帖
与我相关
我的任务
社区描述
软件工程/管理 质量管理/软件测试
功能测试压力测试安全性测试 个人社区 湖南省·长沙市
社区管理员
  • 软件测试
  • 虫无涯
  • 小博测试成长之路
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

欢迎大家加入到软件测试的社区,在这里,希望大家勇于发表自己的看法,欢迎大家分享自己在软件测试工作过程中遇到的问题以及工作经验分享。

1.想转行的小伙伴,遇到问题没有及时回复的,可以私聊小博进行反馈

2.大家对社区有好的建议,都可以在社区发帖进行反馈

推荐大家学习的软件测试入门笔记:软件测试入门学习笔记

试试用AI创作助手写篇文章吧