There are 2 types of expected conditions usage scenarios
- Use directly in assertions
- Used in conjunction with webdriverwait to dynamically wait for elements on the page to appear or disappear
Method Comment
First translate the usage of these methods
title_is
: Determines whether the title of the current page is exactly equal to the expected
title_contains
: Determines whether the title of the current page contains the expected string
presence_of_element_located
: Determining whether an element is added to the DOM tree does not mean that the element must be visible
visibility_of_element_located
: Determines whether an element is visible. visible means that the element is not hidden and the width and height of the element are not equal to 0
visibility_of
: Do the same thing as the above method, just the above method to pass in the locator, this method directly to the element of positioning to be good
presence_of_all_elements_located
: Determines whether at least 1 elements exist in the DOM tree. For example, if the class of N elements on the page is ' column-md-3 ', then this method returns True if there are 1 elements present.
text_to_be_present_in_element
: Determines whether the text in an element contains the expected string
text_to_be_present_in_element_value
: Determines whether the Value property in an element contains the expected string
frame_to_be_available_and_switch_to_it
: Determines whether the frame can switch in, returns True if possible, and switch in, otherwise returns false
invisibility_of_element_located
: Determines whether an element does not exist in the DOM tree or is not visible
element_to_be_clickable
: Determines whether an element is visible and is enable, so it is called clickable
staleness_of
: Wait for an element to be removed from the DOM tree, note that this method also returns TRUE or False
element_to_be_selected
: Determines whether an element is selected, typically used in a drop-down list
element_selection_state_to_be
: Determines whether the selected state of an element matches the expected
element_located_selection_state_to_be
: As in the above method, just the method above is passed to the element that is positioned, and this method is passed into the locator
alert_is_present
: It is an old problem to judge whether there is alert on the page, and many students will ask
A concrete example
The code for the example is here, and it can be run through.
The following code shows some common questions
- How to wait for an element on the page to appear and then manipulate the element
- How to use all the use cases in the UnitTest framework to share 1 browser instances and then close the browser after all use cases are complete
expected_conditions.py
#encoding: Utf-8# Example of how to use Https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/support/expected_ conditions.pyFrom seleniumImport WebdriverFrom Selenium.webdriver.supportImport Expected_conditionsAs ECFrom selenium.webdriver.support.waitImport webdriverwaitFrom selenium.webdriver.common.byImport byImport UnitTest# dr = Webdriver. Phantomjs (' Phantomjs ') Dr= Webdriver. Firefox ()# dr = Webdriver. Chrome () URL=' Http://www.baidu.com ' search_text_field_id=' kw ' Dr.get (URL)Class Ecexample(UnitTest. TestCase):Def Test_title_is(Self):"' Determine if title matches expectations ' Title_is_baidu= Ec.title_is (U ' Baidu a bit, you'll know ')Self.asserttrue (Title_is_baidu (DR))Def Test_titile_contains(Self):"' Determine if the title contains the expected character ' Title_should_contains_baidu= Ec.title_contains (U ' Baidu ')Self.asserttrue (Title_should_contains_baidu (DR))Def test_presence_of_element_located(Self):"' Determine if element appears in the DOM tree ' locator= (by.id, search_text_field_id) search_text_field_should_present= ec.visibility_of_element_located (Locator)"' Dynamic wait 10s, if the 10s element loading is complete then continue to execute the following code, otherwise throw exception ' ' ' Webdriverwait (DR,Until (ec.presence_of_element_located (locator)) Webdriverwait (DR,). Until (ec.visibility_of_element_located (locator))Self.asserttrue (Search_text_field_should_present (DR))Def Test_visibility_of(Self): Search_text_field= dr.find_element_by_id (search_text_field_id) search_text_field_should_visible= Ec.visibility_of (Search_text_field)Self.asserttrue (Search_text_field_should_visible (' Yes '))Def Test_text_to_be_present_in_element(Self): Text_should_present= Ec.text_to_be_present_in_element ((By.name, ' tj_trhao123 ') , ' hao123 ') self.asserttrue (Text_should_present (DR)) @classmethod def teardownclass(KLS): print ' After all ' test ' dr.quit () print ' quit Dr 'if __name__ = = ' __main__ ': Unittest.main ()
Analysis
Taking Title_is as an example
Class Title_is(Object):"" A expectation for checking the title of a page.Title is the expected title, which must was an exact matchReturns True if the title matches, False otherwise. ""Def __init__ (selfself.title = title def __call__ (selfreturn self.title Span class= "OP" >== driver.title
You can see that there title_is
are actually 1 classes __call__
whose methods are defined to return 1 bool values. Therefore, the general usage is
# 实例化the_instance = title_is(‘expected‘)# 直接在实例上调用__call__the_instance(dr) #return True or False
Python Selenium expected_conditions usage examples