driver.find_element_by_xpath 验证页面元素text属性是否包含某个值报错

python +selenium 

当使用 driver.find_element_by_xpath函数,并且使用text属性包含/等于某个值的时候,直接写入变量会提示错误,如下图 

driver.find_element_by_xpath('//*[contains(text(),i]' ).click()

如果直接写上变量名称是没问题的。

 

代码如下:

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')

i="hao123"
driver.find_element_by_xpath('//*[contains(text(),"'+i+'")]' ).click()
#driver.find_element_by_xpath('//*[contains(text(),"hao123")]' ).click()
sleep(1)

最后解决的办法是要将变量名字改成 "'+i+'" 才可以。至于为什么是这种鬼格式,我也不知道o(╥﹏╥)o

driver.find_element_by_xpath('//*[contains(text(),"'+i+'")]' ).click()

原始原因,是由于jquey版本的问题导致的。

 

另外:python2.7如果是英文的还是没问题,如果是中文,把上图代码变量i的hao123修改为新闻,仍然报错,是由于selenium编码的问题,还没有想到合适的解决方法。如果3.6版本是正常的。

 

参考文章:https://blog.csdn.net/tel13259437538/article/details/80474653

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
自动化个人学习第一步笔记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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值