[跟著官方文檔學Selenium][學習筆記][八][WebDriver的Actions接口]

      網友投稿 719 2025-03-31

      用于向web瀏覽器提供虛擬設備輸入的底層接口

      與執行額外驗證的高級元素交互不同,Actions接口提供了對輸入設備的細粒度控制。

      selenium提供3種輸入源:鍵盤設備的鍵位輸入,鼠標、筆或觸摸設備的指針輸入,以及支持滾輪的滾輪輸入。

      鍵盤

      用于與網頁交互的任何關鍵輸入設備的表示形式。

      Keyboard代表一個鍵盤事件。Keyboard操作通過使用底層接口允許我們向web瀏覽器提供虛擬設備輸入。

      Keys

      除了由常規Unicode表示的鍵外,Unicode值還分配給其他鍵以用于selenium。每種語言都有自己的方法來引用這些鍵。

      Key down

      KeyDown用于模擬按下輔助按鍵(CONTROL,SHIFT,ALT)的動作

      import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class keyDownDemo1 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); webDriver.get("https://www.baidu.com/"); //輸入webdriver并按下回車 webDriver.findElement(By.id("kw")).sendKeys("webdriver" + Keys.ENTER); Actions actionProvider = new Actions(webDriver); Action keydown = actionProvider.keyDown(Keys.CONTROL).sendKeys("a").build(); keydown.perform(); } }

      Key up

      keyUp用于模擬輔助按鈕(CONTROL,SHIFT,ALT)彈起或釋放的操作

      import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class keyUpDemo1 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://www.baidu.com"); Actions actions = new Actions(webDriver); WebElement search = webDriver.findElement(By.id("kw")); actions.keyDown(Keys.LEFT_SHIFT).sendKeys(search,"qwerty") .keyUp(Keys.LEFT_SHIFT).sendKeys("qwerty").perform(); } finally { webDriver.quit(); } } }

      Send keys

      這是操作API中的一種便捷方法,它將keyDown和keyUp命令組合到一個操作中。執行此命令與使用element方法略有不同。

      import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class sendKeyDemo1 { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); Actions actions = new Actions(webDriver); webDriver.get("https://www.baidu.com"); WebElement search = webDriver.findElement(By.id("kw")); actions.keyDown(Keys.LEFT_SHIFT).sendKeys(search,"h") .keyUp(Keys.LEFT_SHIFT).sendKeys(search,"elloworld").perform(); webDriver.findElement(By.id("su")).click(); } }

      鼠標

      Mouse表示鼠標事件。鼠標操作是通過使用底層接口執行的,其允許我們向web瀏覽器提供虛擬化的設備輸入操作。

      clickAndHold

      它將移動到該元素,然后在給定元素的中間點擊(不釋放)

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class clickAndHold { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://www.baidu.com"); //保存 WebElement element = webDriver.findElement(By.linkText("登錄")); String id = element.getAttribute("id"); System.out.println(id);//s-top-loginbtn Actions actionProvider = new Actions(webDriver); actionProvider.clickAndHold(element).build().perform(); }finally { webDriver.quit(); } } }

      contextClick

      此方法首先將鼠標移動到元素的位置,然后在給定元素執行上下文點擊(右鍵單擊)。

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class contextClick { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://www.baidu.com"); //存儲登錄按鈕元素 WebElement loginBtn = webDriver.findElement(By.linkText("登錄")); Actions actionProvider = new Actions(webDriver); //在元素上執行操作 actionProvider.contextClick(loginBtn).build().perform(); } finally { webDriver.quit(); } } }

      doubleClick

      它將移動到該元素,并在給定元素的中間雙擊。

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class doubleClick { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://www.baidu.com"); WebElement loginBtn = webDriver.findElement(By.linkText("登錄")); Actions actionProvider = new Actions(webDriver); actionProvider.doubleClick(loginBtn).build().perform(); } finally { webDriver.quit(); } } }

      moveToElement

      此方法將鼠標移到元素的中間,執行此操作時,該元素也會滾動到視圖中。

      import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class moveToElement { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.manage().window().setSize(new Dimension(1024,768)); webDriver.get("http://www.example.com/"); WebElement informationLink = webDriver.findElement(By.linkText("More information...")); Actions actionProvider = new Actions(webDriver); actionProvider.moveToElement(informationLink).build().perform(); } finally { webDriver.quit(); } } }

      moveByOffset

      此方法將鼠標從其當前位置(或0,0)移動給定的偏移量。如果坐標在視圖窗口之外,則鼠標最終將在瀏覽器窗口之外。

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class moveByOffset { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://www.baidu.com"); WebElement loginBtn = webDriver.findElement(By.linkText("登錄")); int xOffset = loginBtn.getRect().getX(); int yOffset = loginBtn.getRect().getY(); Actions actionProvider = new Actions(webDriver); actionProvider.moveByOffset(xOffset,yOffset).build().perform(); } finally { webDriver.quit(); } } }

      dragAndDrop

      此方法首先在源元素上單擊并按住,然后移動到目標元素的位置后釋放鼠標。

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class dragAndDrop { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://crossbrowsertesting.github.io/drag-and-drop"); //存儲'Box A'元素 WebElement sourceEle = webDriver.findElement(By.id("draggable")); //存儲'Box B'元素 WebElement targetEle = webDriver.findElement(By.id("droppable")); Actions actionProvider = new Actions(webDriver); //將sourceEle拖拽到targetEle actionProvider.dragAndDrop(sourceEle,targetEle).build().perform(); } finally { webDriver.quit(); } } }

      dragAndDropBy

      [跟著官方文檔學Selenium][學習筆記][八][WebDriver的Actions接口]

      此方法首先在源元素上單擊并按住,移至給定的偏移量后釋放鼠標。

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class dragAndDropBy { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://crossbrowsertesting.github.io/drag-and-drop"); WebElement sourceEle = webDriver.findElement(By.id("draggable")); WebElement targetEle = webDriver.findElement(By.id("droppable")); int targetEleXOffset = targetEle.getLocation().getX(); int targetEleYOffset = targetEle.getLocation().getY(); Actions actionProvider = new Actions(webDriver); actionProvider.dragAndDropBy(sourceEle,targetEleXOffset,targetEleYOffset).build().perform(); } finally { webDriver.quit(); } } }

      release

      此操作將釋放按下的鼠標左鍵。如果WebElement轉移了,它將釋放給定WebElement上按下的鼠標左鍵。

      import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.interactions.Actions; public class release { public static void main(String[] args) { WebDriver webDriver = new EdgeDriver(); try { webDriver.get("https://crossbrowsertesting.github.io/drag-and-drop"); WebElement sourceEle = webDriver.findElement(By.id("draggable")); WebElement targetEle = webDriver.findElement(By.id("droppable")); Actions actionProvider = new Actions(webDriver); actionProvider.clickAndHold(sourceEle).moveToElement(targetEle).build().perform(); actionProvider.release().build().perform(); } finally { webDriver.quit(); } } }

      滾輪

      滾輪操作將要伴隨Selenium 4.2發布

      Selenium

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:領導力溝通:怎么說和如何說
      下一篇:word文檔中字與字之間的距離怎么調?(word文檔怎樣調字與字之間的距離)
      相關文章
      中文文字幕文字幕亚洲色| 国产亚洲欧洲精品| 亚洲无删减国产精品一区| 国产亚洲?V无码?V男人的天堂| 亚洲精品无码av片| 99久久婷婷国产综合亚洲| 亚洲人成高清在线播放| 老司机亚洲精品影院| 亚洲黄色免费网址| 久久久亚洲欧洲日产国码是AV| 亚洲AV本道一区二区三区四区 | 亚洲精品国偷自产在线| 亚洲精品高清国产一线久久| 国产AV无码专区亚洲Av| 久久久亚洲欧洲日产国码农村| 国产AV无码专区亚洲AV男同 | 亚洲国产日韩一区高清在线| 亚洲色中文字幕无码AV| 国产成人高清亚洲一区久久| 国产亚洲精品AAAA片APP| 亚洲国产成人AV在线播放| 亚洲人成7777影视在线观看| 亚洲国产精品网站久久| 91亚洲精品麻豆| 亚洲精品中文字幕无乱码麻豆| 亚洲不卡1卡2卡三卡2021麻豆| 亚洲国产日韩在线成人蜜芽| 亚洲成aⅴ人在线观看| 亚洲第一成年人网站| 亚洲视频手机在线| 亚洲国产精品人久久电影| 学生妹亚洲一区二区| | 亚洲国产精品久久人人爱| 亚洲嫩草影院在线观看| 91嫩草亚洲精品| 亚洲欧美一区二区三区日产| 亚洲AV成人无码久久WWW| 亚洲AV日韩AV一区二区三曲| 国产午夜亚洲精品不卡免下载| 亚洲国产综合人成综合网站|