Selenium六 find_element_by_xpath()的几种方法

Xpath (XML Path Language),是W3C定义的用来在XML文档中选择节点的语言

一:从根目录/开始

有点像Linux的文件查看,/代表根目录,一级一级的查找,直接子节点,相当于css_selector中的>号

/html/body/div/p 

 

二. 根据元素属性选择:

查找具体的元素,必须在前面输入标准开头//,表示从当前节点寻找所有的后代元素

//div/*     div下面的所有的元素

//div//p     先在整个文档里查找div,再在div里查找p节点(只要在内部,不限定是否紧跟) ;等价于 css_selector里的('div p')

//div/p      p是div的直接子节点; 等价于 css_selector里的('div > p')

//*[@style]   查找所有包含style的所有元素,所有的属性要加@;  等价于 css_selector里的('*[style]')

//p[@spec='len']  必须要加引号;等价于 css_selector里的("p[spec='len']")

//p[@id='kw']    xpath中对于id,class与其他元素一视同仁,没有其他的方法

 

三. 选择第几个节点

//div/p[2]   选择div下的第二个p节点 ;等价于css_selector里的div>p:nth-of-type(2)  符合p类型的第二个节点

//div/*[2]    选择div下第二个元素

//div/p[position()=2]   position()=2   指定第二个位置;  等价于上面的 //div/p[2] 

          position()>=2      位置大于等于2

          position()<2        位置小于2

          position()!=2    位置不等于2

//div/p[last()]    选择div下的倒数第一个p节点; last()倒数第一个

//div/p[last()-1]    选择div下的倒数第二个p节点;

//div/p[position()=last()]     倒数第一个

//div/p[position()=last()-1]     倒数第二个

//div/p[position()>=last()-2]     倒数第一个,第二个,第三个

 

四. 组合选择

//p | //button   选择所有的p和button,等价于css_selector里的 p, button

//input[@id='kw' and @class='su']     选择id=kw 并且 class=su的input元素

 

五. 兄弟节点的选择

相邻后面的兄弟节点的选择:following-sibling::    两个冒号

//div/following-sibling::p    选择div里相邻的p节点

相邻前面的哥哥节点的选择:preceding-sibling::后面加上元素标签        # 此方法在css_selector中没有

相邻前面的弟弟节点的选择   following-sibling:: 后面加上元素标签 

//div/preceding-sibling::p[2]   选择div里前面相邻的第二个节点,不加[2]选择的是前面的所有的p节点

 

六. 选择父节点    

//p[@spec='len']/..    选择p节点的上层节点       此方法在css_selector中没有

//p[@spec='len']/../..   上层节点的上层节点

 

七. 在webelement对象里面使用查找Xpath 查找时,必须使用.指明当前节点

food = driver.find_element_by_id('food')

eles = food.find_elements_by_xpath(".//p")    .指明当前节点

eles = food.find_elements_by_xpath("..")   查找当前节点的父节点

 

  • 32
    点赞
  • 195
    收藏
    觉得还不错? 一键收藏
  • 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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值