<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 如何使用 Selenium WebDriver 驗證工具提示 > 原文: [https://www.guru99.com/verify-tooltip-selenium-webdriver.html](https://www.guru99.com/verify-tooltip-selenium-webdriver.html) 工具提示是當鼠標懸停在網頁上的對象(如鏈接,圖像,按鈕,文本區域等)上時出現的文本。 文本通常會提供有關其出現的對象的更多信息。 傳統上,工具提示是作為元素的“標題”屬性來實現的。 此屬性的值顯示為鼠標懸停時的工具提示。 這是靜態文本,不帶樣式地提供元素的信息。 現在,有許多插件可用于“工具提示”實施。 帶有樣式,渲染,圖像和鏈接的高級工具提示正在使用 JavaScript / JQuery 插件或 CSS 工具提示來實現。 * 為了訪問或驗證使用 HTML“ title”屬性實現的靜態工具提示,我們可以簡單地使用 WebElement 的 getAttribute(“ title”)方法。 將此方法的返回值(即工具提示文本)與期望值進行比較以進行驗證。 * 對于其他形式的工具提示實現,我們將必須使用 Web 驅動程序提供的“高級用戶交互 API”來創建鼠標懸停效果,然后檢索該元素的工具提示。 ## 高級用戶交互 API 簡介: Advanced User Interactions API 為用戶操作提供了 API,例如在網頁上使用鍵盤或鼠標進行拖放,懸停,多選,按鍵和釋放以及其他操作。 您可以參考此鏈接以獲取有關 API 的更多詳細信息。 [https://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/interactions/Actions.html](https://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/interactions/Actions.html) 在這里,讓我們看看如何使用幾個類和方法,這些類和方法將需要將滑塊元素移動一個偏移量。 **步驟 1)**為了使用 API??,需要導入以下包/類: ![](https://img.kancloud.cn/db/4f/db4f825d7a439099b9a1903c81794a8c_408x50.png) **步驟 2)**創建一個“ Actions”類的對象,并構建用戶動作序列。 Actions 類用于構建用戶動作的序列,如 moveToElement(),dragAndDrop()等。API 提供了與用戶動作相關的各種方法。 驅動程序對象作為其構造函數的參數提供。 ![](https://img.kancloud.cn/bd/5b/bd5bb07d814ef6159ad915763a51dac0_717x287.png) **步驟 3)**使用“ Actions”類的 build()方法創建一個 Action Object。 調用 perform()方法以執行由 Actions 對象(此處為 builder)構建的所有操作。 ![](https://img.kancloud.cn/0e/cf/0ecf8a0b3619c76bdce5747edf0f2624_638x255.png) 我們已經看到了如何使用 API??提供的一些用戶 Actions 方法-clickAndHold(element),moveByOffset(10,0),release()。 API 提供了許多此類方法。 有關更多詳細信息,請參見[鏈接](https://seleniumhq.github.io/selenium/docs/api/java/index.html?org/openqa/selenium/interactions/Actions.html)。 ## 如何在 Selenium Webdriver 中獲取工具提示文本 讓我們看一下在簡單場景中訪問和驗證工具提示的演示 * 方案 1:使用“ title”屬性實現工具提示 * 方案 2:工具提示是使用 jQuery 插件實現的。 ### **方案 1:HTML“標題”屬性** 對于這種情況,讓我們以示例網站- [http://demo.guru99.com/test/social-icon.html](http://demo.guru99.com/test/social-icon.html) 為例。 我們將嘗試驗證頁面右上方的“ github”圖標的工具提示。 ![](https://img.kancloud.cn/99/c6/99c68802f00a8f820180525c8e2c63e1_599x50.png) 為了做到這一點,我們將首先找到該元素并獲取其“ title”屬性,并使用預期的工具提示文本進行驗證。 由于我們假設工具提示位于“ title”屬性中,因此我們甚至沒有自動實現鼠標懸停效果,而只是使用“ getAttribute()”方法檢索該屬性的值。 ![](https://img.kancloud.cn/d4/f4/d4f4d8255572a69caa5bb84072af7e5f_1063x503.png) **這是代碼** ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.*; public class ToolTip { public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/social-icon.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get(baseUrl); String expectedTooltip = "Github"; // Find the Github icon at the top right of the header WebElement github = driver.findElement(By.xpath(".//*[@class='soc-ico show-round']/a[4]")); //get the value of the "title" attribute of the github icon String actualTooltip = github.getAttribute("title"); //Assert the tooltip's value is as expected System.out.println("Actual Title of Tool Tip"+actualTooltip); if(actualTooltip.equals(expectedTooltip)) { System.out.println("Test Case Passed"); } driver.close(); } } ``` **代碼**的說明 1. 找到代表“ github”圖標的 WebElement。 2. 使用 getAttribute()方法獲取其“ title”屬性。 3. 將值與預期的工具提示值進行對照。 ### 方案 2:JQuery 插件: 有很多 JQuery 插件可用于實現工具提示,并且每個插件都有略有不同的實現形式。 一些插件希望工具提示 HTML 始終顯示在工具提示適用的元素旁邊,而其他插件則創建一個動態的“ div”標簽,將其懸停在元素上時會即時顯示。 對于我們的演示,讓我們考慮工具提示實現的“ jQuery Tools Tooltip”方法。 在 URL – [http://demo.guru99.com/test/tooltip.html](http://demo.guru99.com/test/tooltip.html) 中,您可以看到演示,將鼠標懸停在“立即下載”上,我們將獲得帶有圖像的高級工具提示, 標注背景,表格以及其中的可點擊鏈接。 ![](https://img.kancloud.cn/7d/b2/7db2bc97b87bfac926826e520ff1018a_805x336.png) 如果查看下面的源代碼,您會發現代表工具提示的 div 標簽始終位于“立即下載”鏈接的標簽旁邊。 但是,下面的 script 標記內的代碼控制何時需要彈出。 ![](https://img.kancloud.cn/dc/10/dc106782faf4af7d5e3eabb3bca0ed3a_968x373.png) 讓我們嘗試僅驗證工具提示中的鏈接文本以進行此處的演示。 我們將首先找到與“立即下載”相對應的 WebElement。 然后使用 Interactions API,我們將移至元素(鼠標懸停)。 接下來,我們將在顯示的工具提示中找到與鏈接相對應的 WebElement,并根據預期的文本進行驗證。 ![](https://img.kancloud.cn/ce/7a/ce7ad120e0c79018a81758fc3fbe1feb_1062x398.png) **Here is the code** ``` import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.*; public class JqueryToolTip { public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/tooltip.html"; System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String expectedTooltip = "What's new in 3.2"; driver.get(baseUrl); WebElement download = driver.findElement(By.xpath(".//*[@id='download_now']")); Actions builder = new Actions (driver); builder.clickAndHold().moveToElement(download); builder.moveToElement(download).build().perform(); WebElement toolTipElement = driver.findElement(By.xpath(".//*[@class='box']/div/a")); String actualTooltip = toolTipElement.getText(); System.out.println("Actual Title of Tool Tip "+actualTooltip); if(actualTooltip.equals(expectedTooltip)) { System.out.println("Test Case Passed"); } driver.close(); } } ``` **代碼說明** 1. 找到與將鼠標懸停的元素“立即下載”相對應的 WebElement。 2. 使用 Interactions API,將鼠標懸停在“立即下載”上。 3. 假設顯示了工具提示,請找到與工具提示內的鏈接相對應的 WebElement,即“ a”標簽。 4. 根據我們存儲在“ expectedToolTip”中的期望值,驗證使用 getText()檢索到的鏈接的工具提示文本 ### 摘要: 在本教程中,您學習了如何使用 Selenium Web 驅動程序訪問工具提示。 * 工具提示以不同的方式實現– * 基本實現基于 HTML 的“ title”屬性。 getAttribute(title)獲取工具提示的值。 * 其他工具提示實現(例如 JQuery),CSS 工具提示需要 Interactions API 來創建鼠標懸停效果 * 進階使用者互動 API * Actions 類的 moveToElement(element)用于將鼠標懸停在元素上。 * Actions 類的 Build()方法將用戶操作序列構建到 Action 對象中。 * Action 類的 Perform()一次執行所有用戶操作序列。 * 為了驗證工具提示,我們必須首先將鼠標懸停在元素上,然后找到與工具提示相對應的元素,并獲取其文本或其他值以對照預期值進行驗證。
                  <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>

                              哎呀哎呀视频在线观看