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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 鼠標單擊&鍵盤事件:Selenium Webdriver 中的操作類 > 原文: [https://www.guru99.com/keyboard-mouse-events-files-webdriver.html](https://www.guru99.com/keyboard-mouse-events-files-webdriver.html) 在本教程中,我們將學習在 Selenium Webdriver 中處理鍵盤和鼠標事件 ## 處理鍵盤&鼠標事件 使用**高級用戶交互 API** 處理特殊的鍵盤和鼠標事件。 它包含執行這些事件時所需的**操作**和**操作**類。 以下是 Actions 類提供的最常用的鍵盤和鼠標事件。 | **方法** | **說明** | | **clickAndHold()** | 在當前鼠標位置單擊(不釋放)。 | | **contextClick()** | 在當前鼠標位置執行上下文單擊。 (右鍵單擊鼠標動作) | | **doubleClick()** | 在當前鼠標位置上雙擊。 | | **dragAndDrop(源,目標)** | 在源元素的位置單擊并按住,移至目標元素的位置,然后釋放鼠標。 **參數:** 源元素,用于模擬按下按鈕的位置。 目標元素,可移動至并釋放鼠標。 | | **dragAndDropBy(source,x-offset,y-offset)** | 在源元素的位置單擊并按住,移動給定的偏移量,然后釋放鼠標。 **參數**: 源元素,用于模擬按下按鈕的位置。 xOffset-水平移動偏移。 yOffset-垂直移動偏移。 | | **keyDown(modifier_key)** | 執行修改鍵。 不釋放修飾鍵-后續的交互可能會假定它一直處于按下狀態。 **參數**: 修飾符鍵-任何修飾鍵(Keys.ALT,Keys.SHIFT 或 Keys.CONTROL) | | **keyUp(修飾符 _key)** | 執行密鑰釋放。 **參數**: 修飾符鍵-任何修飾鍵(Keys.ALT,Keys.SHIFT 或 Keys.CONTROL) | | **moveByOffset(x-offset,y-offset)** | 將鼠標從其當前位置(或 0,0)移動給定的偏移量。 **參數**: x-offset-水平偏移。 負值表示將鼠標左移。 y-offset-垂直偏移。 負值表示向下移動鼠標。 | | **moveToElement(toElement)** | 將鼠標移到元素的中間。 **參數**: 要移動到的 Element 元素。 | | **release()** | 在當前鼠標位置釋放按下的鼠標左鍵 | | **sendKeys(onElement,charsequence)** | 向元素發送一系列擊鍵。 **參數**: onElement-將接收擊鍵的元素,通常是文本字段 charsequence-表示擊鍵序列的任何字符串值 被發送 | 在下面的示例中,我們將使用 moveToElement()方法將鼠標懸停在一個 Mercury Tours 的表行上。 請參見下面的示例。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/48/e1/48e17f76f657f1c3684e05c4e3b89cc5_296x229.png) 上面顯示的單元格是< TR >元素的一部分。 如果未懸停,則其顏色為#FFC455(橙色)。 懸停后,單元格的顏色變為透明。 它變成與整個橙色表的藍色背景相同的顏色。 **步驟 1:**導入**動作**和**動作**類。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/00/7c/007c1b9767dfe227f8a653ea68f695ba_351x43.png) **步驟 2:**實例化一個新的 Actions 對象。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/1c/3c/1c3c14c4e91d1110594fcc7882cab2b9_294x28.png) **步驟 3:**在步驟 2 中使用 Actions 對象實例化一個 Action。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/88/f5/88f547e2f664ebc4e64a06cf5afcb66e_267x59.png) 在這種情況下,我們將使用 moveToElement()方法,因為我們只是將鼠標懸停在“ Home”鏈接上。 build()方法始終是最終使用的方法,因此所有列出的動作將被編譯為一個步驟。 **步驟 4:**在執行我們在步驟 3 中設計的 Action 對象時,請使用 perform()方法。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/e7/08/e708ac061590806f6a8250388ed41819_183x29.png) 以下是整個 WebDriver 代碼,用于在鼠標懸停之前和之后檢查< TR >元素的背景顏色。 ``` package newproject; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class PG7 { public static void main(String[] args) { String baseUrl = "http://demo.guru99.com/test/newtours/"; System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement link_Home = driver.findElement(By.linkText("Home")); WebElement td_Home = driver .findElement(By .xpath("//html/body/div" + "/table/tbody/tr/td" + "/table/tbody/tr/td" + "/table/tbody/tr/td" + "/table/tbody/tr")); Actions builder = new Actions(driver); Action mouseOverHome = builder .moveToElement(link_Home) .build(); String bgColor = td_Home.getCssValue("background-color"); System.out.println("Before hover: " + bgColor); mouseOverHome.perform(); bgColor = td_Home.getCssValue("background-color"); System.out.println("After hover: " + bgColor); driver.close(); } } ``` 下面的輸出清楚地表明,將鼠標懸停后背景顏色變為透明。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/70/0f/700f0c16b35cfbcae2f52309e27b9bac_260x73.png) ## 建立一系列多重動作 **您可以使用 Action 和 Actions 類**來構建一系列操作。 只需記住使用 build()方法關閉該系列。 考慮下面的示例代碼。 ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/d5/2e/d52e678c3c66561b19794dbc4eac4922_552x312.png) ``` public static void main(String[] args) { String baseUrl = "http://www.facebook.com/"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement txtUsername = driver.findElement(By.id("email")); Actions builder = new Actions(driver); Action seriesOfActions = builder .moveToElement(txtUsername) .click() .keyDown(txtUsername, Keys.SHIFT) .sendKeys(txtUsername, "hello") .keyUp(txtUsername, Keys.SHIFT) .doubleClick(txtUsername) .contextClick() .build(); seriesOfActions.perform() ; } ``` ![Keyboard & Mouse Event using Action Class in Selenium Webdriver](https://img.kancloud.cn/2f/1e/2f1e1b7c2d3cb72e6a6d1885aaff8146_340x282.png) **摘要** * 使用 AdvancedUserInteractions API 處理特殊的鍵盤和鼠標事件。 * 常用的關鍵字和鼠標事件是 doubleClick(),keyUp,dragAndDropBy,contextClick & sendKeys。
                  <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>

                              哎呀哎呀视频在线观看