Analyzing Selenium2 text (text_to_be_present_in_element)

In doing judgment result, they often want to determine whether there is an element specified in the text, such as page after logging in to determine whether the account is in the user's user name.

In the previous case login and write a simple method, but it is not common, there is a method in the EC module is designed to determine the presence of the specified text in the element: text_to_be_present_in_element.

Another similar complex method of determining the value of the element value: text_to_be_present_in_element_value.

 

First, source code analysis

class text_to_be_present_in_element(object):
    """ An expectation for checking if the given text is present in the
    specified element.
    locator, text
    """

    '' 'Translation: that the specified text, the parameter determining elements: Locator, text' ''
    DEF the __init __ (Self, Locator, text_):
        self.locator = Locator
        self.text = text_

    DEF the __call __ (Self, Driver):
        the try:
            element_text = _find_element (Driver, self.locator) .text
            return self.text element_text in
        the except StaleElementReferenceException:
            return False

1. Translation: determining whether there is a specified text element, two parameters: locator, text

2 .__ call__ in return is a Boolean value: Ture and False

 

Second, determine the text

1. The judgment on Baidu Home, "sticky rice" there is this element of the text button: rice

2.locator positioning method parameter

3.text parameter is the desired value

 

Third, failures

1. If the determination fails, it returns False

 

四、判断value的方法
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

1. The value of this method with the above, only in this judgment is the value of

2. To give a simple example, determine the value of Baidu search button value

Guess you like

Origin www.cnblogs.com/baoshilin/p/12507993.html