Selenium2+python Automation 42-judging Element (expected_conditions) "Reprint"

Source: Internet
Author: User

Objective

Often a small partner asked, how to determine whether an element exists, how to determine the alert pop-up window, how to judge the dynamic elements and so on a series of judgments, in the Selenium Expected_conditions module collection of a series of scene judgment method, these methods are every interview must TEST!!!

Expected_conditions General also referred to as EC, this article first describes what the next features, the subsequent update will be a single to introduce.

First, function introduction and translation

Title_is: Determines whether the title of the current page is exactly equal to (= =) Expected string, returns a Boolean value

Title_contains: Determines whether the title of the current page contains the expected string, returns a Boolean value

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 method above to pass in the locator, this method directly to the element of the positioning to be good

Presence_of_all_elements_located: Determines if 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: Determine if the frame can switch in, if possible, return true and switch in, otherwise return 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: Determine whether an element is selected in accordance with the expected state

Element_located_selection_state_to_be: As with the above method, only the method above is passed to the element that is positioned, and this method is passed into the locator

Alert_is_present: Determine if alert is present on the page

Second, view the source code and comments

1. Open python in this directory L can find: lib\site-packages\selenium\webdriver\support\expected_conditions.py

From selenium.common.exceptions import nosuchelementexception
From selenium.common.exceptions import nosuchframeexception
From selenium.common.exceptions import staleelementreferenceexception
From selenium.common.exceptions import webdriverexception
From selenium.common.exceptions import noalertpresentexception

"""
* Canned "expected Conditions" which is generally useful within webdriver
* Tests.
"""


Class Title_is (object):
"" A expectation for checking the title of a page.
Title is the expected title, which must was an exact match
Returns True if the title matches, False otherwise. ""
def __init__ (self, title):
Self.title = Title

def __call__ (self, driver):
return Self.title = = Driver.title


Class Title_contains (object):
"" "an expectation for checking the title contains a case-sensitive
Substring. Title is the fragment of title expected
Returns True when the title matches, False otherwise
"""
def __init__ (self, title):
Self.title = Title

def __call__ (self, driver):
Return Self.title in Driver.title


Class Presence_of_element_located (object):
"" "an expectation for checking this an element was present on the DOM
of a page. This does not necessarily mean, the element is visible.
Locator-used to find the element
Returns the Webelement once it is located
"""
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
Return _find_element (Driver, Self.locator)


Class Visibility_of_element_located (object):
"" "an expectation for checking so an element was present on the DOM of a
Page and visible. Visibility means the element is not only displayed
But also have a height and width that's greater than 0.
Locator-used to find the element
Returns the Webelement once it is located and visible
"""
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
Try
Return _element_if_visible (_find_element (Driver, self.locator))
Except staleelementreferenceexception:
Return False


Class Visibility_of (object):
"" "an expectation-checking that an element, known-to is present on the
DOM of a page, is visible. Visibility means the element is not only
Displayed but also have a height and width that's greater than 0.
Element is the Webelement
Returns the (same) Webelement once it is visible
"""
def __init__ (self, Element):
Self.element = Element

def __call__ (self, ignored):
Return _element_if_visible (self.element)


def _element_if_visible (element, visibility=true):
return element if element.is_displayed () = = Visibility Else False


Class Presence_of_all_elements_located (object):
"" "an expectation for checking, there is at least one element present
On a Web page.
Locator is used to find the element
Returns the list of webelements once they is located
"""
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
Return _find_elements (Driver, Self.locator)


Class Visibility_of_any_elements_located (object):
"" "an expectation for checking, there is at least one element visible
On a Web page.
Locator is used to find the element
Returns the list of webelements once they is located
"""
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
return [element for element in _find_elements (driver, Self.locator) if _element_if_visible (element)]


Class Text_to_be_present_in_element (object):
"" "an expectation for checking if the given text was 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


Class Text_to_be_present_in_element_value (object):
"""
An expectation for checking if the given text was 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


Class Frame_to_be_available_and_switch_to_it (object):
"" "an expectation for checking whether the given frame was available to
Switch to. If the frame is available it switches the given driver to the
Specified frame.
"""
def __init__ (self, locator):
Self.frame_locator = Locator

def __call__ (self, driver):
Try
If Isinstance (self.frame_locator, tuple):
Driver.switch_to.frame (_find_element (Driver,
Self.frame_locator))
Else
Driver.switch_to.frame (Self.frame_locator)
Return True
Except nosuchframeexception:
Return False


Class Invisibility_of_element_located (object):
"" "an expectation for checking this an element was either invisible or not
Present on the DOM.

Locator used to find the element
"""
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
Try
Return _element_if_visible (_find_element (Driver, self.locator), False)
Except (Nosuchelementexception, staleelementreferenceexception):
# in the case of nosuchelement, returns True because the element is
# not present in DOM. The try block checks if the element is present
# but it is invisible.
# in the case of staleelementreference, returns true because stale
# element reference implies that element is no longer visible.
Return True


Class Element_to_be_clickable (object):
"" "an expectation for checking a element is the visible and enabled such that
You can click it. "" "
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
element = visibility_of_element_located (self.locator) (driver)
If element and element.is_enabled ():
return element
Else
Return False


Class Staleness_of (object):
"" "Wait until an element was no longer attached to the DOM.
element is the element to and wait for.
Returns False if the element is still attached to the DOM, true otherwise.
"""
def __init__ (self, Element):
Self.element = Element

def __call__ (self, ignored):
Try
# Calling any method forces a staleness check
Self.element.is_enabled ()
Return False
Except staleelementreferenceexception:
Return True


Class Element_to_be_selected (object):
"" An expectation-checking the selection is selected.
Element is Webelement object
"""
def __init__ (self, Element):
Self.element = Element

def __call__ (self, ignored):
Return self.element.is_selected ()


Class Element_located_to_be_selected (object):
"" "an expectation-the element to being located is selected.
Locator is a tuple of (by, Path) "" "
def __init__ (self, locator):
Self.locator = Locator

def __call__ (self, driver):
Return _find_element (Driver, self.locator). is_selected ()


Class Element_selection_state_to_be (object):
"" An expectation for checking if the given element is selected.
Element is Webelement object
Is_selected is a Boolean. "
"""
def __init__ (self, Element, is_selected):
Self.element = Element
self.is_selected = is_selected

def __call__ (self, ignored):
return self.element.is_selected () = = self.is_selected


Class Element_located_selection_state_to_be (object):
"" "An expectation-locate an element and a check if the selection state
Specified is in this state.
Locator is a tuple of (by, Path)
Is_selected is a Boolean
"""
def __init__ (self, Locator, is_selected):
Self.locator = Locator
self.is_selected = is_selected

def __call__ (self, driver):
Try
element = _find_element (driver, Self.locator)
return element.is_selected () = = self.is_selected
Except staleelementreferenceexception:
Return False


Class Alert_is_present (object):
"" "Expect an alert to be present." "" "
def __init__ (self):
Pass

def __call__ (self, driver):
Try
Alert = Driver.switch_to.alert
Alert.text
Return alert
Except noalertpresentexception:
Return False


def _find_element (Driver, by):
"" "Looks up an element. Logs and re-raises ' webdriverexception '
If thrown. "" "
Try
Return Driver.find_element (*by)
Except Nosuchelementexception as E:
Raise E
Except Webdriverexception as E:
Raise E


def _find_elements (Driver, by):
Try
Return Driver.find_elements (*by)
Except Webdriverexception as E:
Raise E

This article's judgment method and the scene many, first post out, the back slowly updates, the detailed explanation each function scene and the usage.

These methods are to write a good automation script, improve the performance of the way, want to do automation, you have to master.

Selenium2+python Automation 42-judging Element (expected_conditions) "Reprint"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.