<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] 前言 經常有小伙伴問,如何判斷一個元素是否存在,如何判斷alert彈窗出來了,如何判斷動態的元素等等一系列的判斷,在selenium的expected\_conditions模塊收集了一系列的場景判斷方法,這些方法是逢面試必考的!!! expected\_conditions一般也簡稱EC,本篇先介紹下有哪些功能,后續更新中會單個去介紹。 <br /> ## 一、功能介紹和翻譯 * title\_is: 判斷當前頁面的title是否完全等于(==)預期字符串,返回布爾值 * title\_contains : 判斷當前頁面的title是否包含預期字符串,返回布爾值 * presence\_of\_element\_located : 判斷某個元素是否被加到了dom樹里,并不代表該元素一定可見 * visibility\_of\_element\_located : 判斷某個元素是否可見. 可見代表元素非隱藏,并且元素的寬和高都不等于0 * visibility\_of : 跟上面的方法做一樣的事情,只是上面的方法要傳入locator,這個方法直接傳定位到的element就好了 * presence\_of\_all\_elements\_located : 判斷是否至少有1個元素存在于dom樹中。舉個例子,如果頁面上有n個元素的class都是'column-md-3',那么只要有1個元素存在,這個方法就返回True * text\_to\_be\_present\_in\_element : 判斷某個元素中的text是否 包含 了預期的字符串 * text\_to\_be\_present\_in\_element\_value : 判斷某個元素中的value屬性是否 包含 了預期的字符串 * frame\_to\_be\_available\_and\_switch\_to\_it : 判斷該frame是否可以switch進去,如果可以的話,返回True并且switch進去,否則返回False * invisibility\_of\_element\_located : 判斷某個元素中是否不存在于dom樹或不可見 * element\_to\_be\_clickable : 判斷某個元素中是否可見并且是enable的,這樣的話才叫clickable * staleness\_of : 等某個元素從dom樹中移除,注意,這個方法也是返回True或False * element\_to\_be\_selected : 判斷某個元素是否被選中了,一般用在下拉列表 * element\_selection\_state\_to\_be : 判斷某個元素的選中狀態是否符合預期 * element\_located\_selection\_state\_to\_be : 跟上面的方法作用一樣,只是上面的方法傳入定位到的element,而這個方法傳入locator * alert\_is\_present : 判斷頁面上是否存在alert <br /> ## 二、查看源碼和注釋 1. 打開python里這個目錄l可以找到:Lib\\site-packages\\selenium\\webdriver\\support\\expected\_conditions.py ![](http://images2015.cnblogs.com/blog/1070438/201703/1070438-20170312171733248-1373947423.png) ``` 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 are generally useful within webdriver ?* tests. """ class title_is(object): ??? """An expectation for checking the title of a page. ??? title is the expected title, which must be 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 that 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 that an element is present on the DOM ??? of a page. This does not necessarily mean that 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 that an element is present on the DOM of a ??? page and visible. Visibility means that the element is not only displayed ??? but also has a height and width that is 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 for checking that an element, known to be present on the ??? DOM of a page, is visible. Visibility means that the element is not only ??? displayed but also has a height and width that is 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 that 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 are 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 that 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 are 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 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 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 class frame_to_be_available_and_switch_to_it(object): ??? """ An expectation for checking whether the given frame is 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 that an element is 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 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 an element is 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 is no longer attached to the DOM. ??? element is the element to 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 for 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 for the element to be 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 to locate an element and check if the selection state ??? specified is in that 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 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看