Pthon selenium expected_conditions usage example

Original Link: https://blog.csdn.net/xugexuge/article/details/88065287

scene

There are two scenarios for using Expected Conditions

  • Use directly in an assertion
  • Use with WebDriverWait to dynamically wait for elements to appear or disappear on the page

Method Notes

Translate the usage of these methods first

  • title_is: Determines if the title of the current page is exactly equal to what is expected

  • title_contains: Determines if the title of the current page contains the expected string

  • presence_of_element_located: Determining whether an element has been added to the dom tree does not mean that it must be visible

  • visibility_of_element_located: Determines whether an element is visible. Visibility represents an element that is not hidden, and neither its width nor height is equal to 0

  • visibility_of: Does the same thing as the above method, except that the above method passes in the locator, which passes directly to the element being located

  • presence_of_all_elements_located: Determines if at least one element exists in the dom tree.For example, if the class es of n elements on a page are'column-md-3', this method returns True if only one element exists

  • text_to_be_present_in_element: Determines if the text in an element contains the expected string

  • text_to_be_present_in_element_value: Determines if the value attribute in an element contains the expected string

  • frame_to_be_available_and_switch_to_it: Determines if the frame can be switched in, returns True if it can, and switches 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 enable d, which is called clickable

  • An element such as staleness_of: is removed from the dom tree, note that this method also returns True or False

  • element_to_be_selected: Determines whether an element is selected, commonly used in drop-down lists

  • element_selection_state_to_be: Determines whether an element's selected state is as expected

  • element_located_selection_state_to_be: Just like the above method does, it just passes in the positioned element, and this method passes in the locator

  • alert_is_present: Determining if alert exists on a page is an old question that many students will ask

Specific examples

The code for the example is Here And can be run and passed.

The following code demonstrates some common questions

  • How to wait for an element to appear on the page before you manipulate it
  • How to share a browser instance for all use cases in the unittest framework and then close the browser after all use cases have ended

expected_conditions.py

  1. #encoding:utf-8
  2. # example of how to use https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/support/expected_conditions.py
  3. from selenium import webdriver
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. import unittest
  8. # dr = webdriver.PhantomJS('phantomjs')
  9. dr = webdriver.Firefox()
  10. # dr = webdriver.Chrome()
  11. url = 'http://www.baidu.com'
  12. search_text_field_id = 'kw'
  13. dr.get(url)
  14. class ECExample(unittest.TestCase):
  15. def test_title_is(self):
  16. ''' judge title Is it as expected '''
  17. title_is_baidu = EC.title_is(u'Baidu once, you know')
  18. self.assertTrue(title_is_baidu(dr))
  19. def test_titile_contains(self):
  20. ''' judge title Does it contain the expected characters '''
  21. title_should_contains_baidu = EC.title_contains(u'Baidu')
  22. self.assertTrue(title_should_contains_baidu(dr))
  23. def test_presence_of_element_located(self):
  24. ''' judge element Is it present dom tree '''
  25. locator = (By.ID, search_text_field_id)
  26. search_text_field_should_present = EC.visibility_of_element_located(locator)
  27. ''' Dynamic Wait 10 s,If 10 s within element Continue executing the following code when loading is complete, otherwise throw an exception '''
  28. WebDriverWait(dr, 10).until(EC.presence_of_element_located(locator))
  29. WebDriverWait(dr, 10).until(EC.visibility_of_element_located(locator))
  30. self.assertTrue(search_text_field_should_present(dr))
  31. def test_visibility_of(self):
  32. search_text_field = dr.find_element_by_id(search_text_field_id)
  33. search_text_field_should_visible = EC.visibility_of(search_text_field)
  34. self.assertTrue(search_text_field_should_visible('yes'))
  35. def test_text_to_be_present_in_element(self):
  36. text_should_present = EC.text_to_be_present_in_element((By.NAME, 'tj_trhao123'), 'hao123')
  37. self.assertTrue(text_should_present(dr))
  38. @classmethod
  39. def tearDownClass(kls):
  40. print 'after all test'
  41. dr.quit()
  42. print 'quit dr'
  43. if __name__ == '__main__':
  44. unittest.main()

Analysis

with title_is take as an example

  1. class title_is(object):
  2. """An expectation for checking the title of a page.
  3. title is the expected title, which must be an exact match
  4. returns True if the title matches, false otherwise."""
  5. def __init__(self, title):
  6. self.title = title
  7. def __call__(self, driver):
  8. return self.title == driver.title

You can seetitle_isIt's actually one class,his__call__Method is defined as returning 1 bool Value.Therefore, the general usage is

  1. # instantiation
  2. the_instance = title_is('expected')
  3. # Call u call_u directly on the instance
  4. the_instance(dr) #return True or False
                                </div>
                </div>

Keywords: Selenium Attribute encoding github

Added by maverick3d on Fri, 23 Aug 2019 06:05:47 +0300