二十五、爬取毛豆新車的數據
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
此方法首先在源元素上單擊并按住,移至給定的偏移量后釋放鼠標。
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小時內刪除侵權內容。