<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] 前言 在做結果判斷的時候,經常想判斷某個元素中是否存在指定的文本,如登錄后判斷頁面中是賬號是否是該用戶的用戶名。 在前面的登錄案例中,寫了一個簡單的方法,但不是公用的,在EC模塊有個方法是可以專門用來判斷元素中存在指定文本的:text\_to\_be\_present\_in\_element。 另外一個差不多復方法判斷元素的value值:text\_to\_be\_present\_in\_element\_value。 <br /> ## 一、源碼分析 ``` 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 1. 翻譯:判斷元素中是否存在指定的文本,兩個參數:locator, text 2. __call__里返回的是布爾值:Ture和False ``` <br /> ## 二、判斷文本 1. 判斷百度首頁上,“學術”按鈕這個元素中存在文本:學術 ![Snipaste_2020-09-10_14-59-03.png](http://i.loli.net/2020/09/10/68K7Qdkmg1szp9o.png) 2. locator參數是定位的方法 3. text參數是期望的值 ![Snipaste_2020-09-10_15-00-51.png](http://i.loli.net/2020/09/10/sIvaTSBMufjZ5Vp.png) <br /> ## 三、失敗案例 1. 如果判斷失敗,就返回False ![Snipaste_2020-09-10_15-12-35.png](http://i.loli.net/2020/09/10/2iDHZ3Ayvdnmr4k.png) <br /> ## 四、判斷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. 這個方法跟上面的差不多,只是這個是判斷的value的值 ``` 2. 這里舉個簡單案例,判斷百度搜索按鈕的value值 ![Snipaste_2020-09-10_15-20-25.png](http://i.loli.net/2020/09/10/eqz8boROBEpcCtS.png) <br /> ## 五、參考代碼 ``` from selenium import webdriver from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() url = "https://www.baidu.com" driver.get(url) locator = ("css selector", ".mnav:nth-child(6)") text = "學術" result = EC.text_to_be_present_in_element(locator, text)(driver) print(result) # 下面是失敗的案例 text1 = "學術網" result1 = EC.text_to_be_present_in_element(locator, text1)(driver) print(result1) ```
                  <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>

                              哎呀哎呀视频在线观看