前言
在做结果判断的时候,经常想判断某个元素中是否存在指定的文本,如登录后判断页面中账号是否是该用户的用户名。
在前面的登录案例中,写了一个简单的方法,但不是公用的,在EC模块有个方法是可以专门用来判断元素中存在指定文本的:text_to_be_present_in_element。
另外一个差不多的方法判断元素的value值:text_to_be_present_in_element_value。
一、源码分析
#判断元素中是否存在指定的文本,参数:locator,text
#__call__里返回的是布尔值:True和False
class text_to_be_present_in_element(object): """ An expectation for checking if the given text is present in the specified element. locator, text """ def __init__(self, locator, text_): self.locator = locator self.text = text_ def __call__(self, driver): try: element_text = _find_element(driver, self.locator).text return self.text in element_text except StaleElementReferenceException: return False
二、判断文本
1.判断百度首页上,“新闻”按钮这个元素中存在文本:新闻
2.locator参数是定位的方法(定位方法与find_element()一致)
3.text参数是期望的值
# coding:utf-8 from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver=webdriver.Chrome() driver.get("https://www.baidu.com") print(driver.find_element("xpath","//div[@id='s-top-left']/a[1]").text) #成功 rs=EC.text_to_be_present_in_element(("xpath","//div[@id='s-top-left']/a[1]"),"新闻")(driver) print(rs) #失败 rs1=EC.text_to_be_present_in_element(("xpath","//div[@id='s-top-left']/a[1]"),"地图")(driver) print(rs1)
运行结果:
新闻 True False
三、判断value的方法
1.源码如下
class text_to_be_present_in_element_value(object): """ An expectation for checking if the given text is present in the element's locator, text """ def __init__(self, locator, text_): self.locator = locator self.text = text_ def __call__(self, driver): try: element_text = _find_element(driver, self.locator).get_attribute("value") if element_text: return self.text in element_text else: return False except StaleElementReferenceException: return False
2.这个方法跟上面的差不多,只是这个是判断的value的值
3.这里举个简单案例,判断百度搜索按钮的value值(注意无value属性不会报错,会返回False)
参考代码:
# coding:utf-8 from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC driver=webdriver.Chrome() driver.get("https://www.baidu.com") rs=EC.text_to_be_present_in_element_value(("id","su"),"百度一下")(driver) print(rs) rs1=EC.text_to_be_present_in_element_value(("xpath","//div[@id='s-top-left']/a[1]"),"新闻")(driver) print(rs1)
运行结果:
True False
四、小结
EC.text_to_be_present_in_element(locator,text)
EC.text_to_be_present_in_element_value(locator,text)
locator的写法与find_element()的语法一致;
# coding:utf-8 from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC driver=webdriver.Chrome() driver.get("https://www.baidu.com") rs=EC.text_to_be_present_in_element_value(("id","su"),"百度一下")(driver) print(rs) #True rrs=driver.find_element("id","su") print(rrs.get_attribute("value")) #百度一下 print(dir(rrs),rrs.text) #rrs的属性和方法,空 #下面text无内容,返回False print(EC.text_to_be_present_in_element(("id","su"),"百度一下")(driver)) #False #下面无value属性,不报错,返回False rs1=EC.text_to_be_present_in_element_value(("xpath","//div[@id='s-top-left']/a[1]"),"新闻")(driver) print(rs1) #False print(driver.find_element("xpath","//div[@id='s-top-left']/a[1]").text) #新闻 print(EC.text_to_be_present_in_element(("xpath","//div[@id='s-top-left']/a[1]"),"新闻")(driver)) #True
运行结果:
True 百度一下 ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_execute', '_id', '_parent', '_upload', '_w3c', 'clear', 'click', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'get_attribute', 'get_property', 'id', 'is_displayed', 'is_enabled', 'is_selected', 'location', 'location_once_scrolled_into_view', 'parent', 'rect', 'screenshot', 'screenshot_as_base64', 'screenshot_as_png', 'send_keys', 'size', 'submit', 'tag_name', 'text', 'value_of_css_property'] False False 新闻 True