• Selenium Video Tutorials

Selenium WebDriver - 筆事件



Selenium Webdriver 可用於執行類似於筆觸的指標輸入,以與頁面上的 Web 元素進行通訊。這使用Actions 類執行。筆事件僅在 Chromium 瀏覽器中可用。

什麼是筆操作?

筆類似於指標輸入,具有與滑鼠相似的特性。它還可以具有與觸控筆不同的事件屬性。滑鼠有五個按鈕,而筆只有三個按鈕:0 表示觸控接觸(類似於左鍵單擊)、2 表示筆筒按鈕(類似於右鍵單擊)和 5 表示橡皮擦按鈕(驅動程式目前尚不支援)。

執行筆事件的語法

WebElement a = driver.findElement(By.xpath("xpath of pointer area"));
new Actions(driver)
   .setActivePointer(PointerInput.Kind.PEN, "default pen")
   .moveToElement(a)
   .clickAndHold()
   .moveByOffset(5, 5)
   .release()
   .perform();

執行指標事件屬性的語法

WebElement a = driver.findElement(By.xpath("xpath of pointer area"));
PointerInput p = 
new PointerInput(PointerInput.Kind.PEN, "default pen");
PointerInput.PointerEventProperties eventProperties = 
PointerInput.eventProperties()
   .setTiltX(-82)
   .setTiltY(10)
   .setTwist(98);
PointerInput.Origin o = PointerInput.Origin.fromElement(a);

Sequence act = new Sequence(p, 0)
   .addAction(p.createPointerMove(Duration.ZERO, origin, 0, 5))
   .addAction(p.createPointerDown(0))
   .addAction(p.createPointerMove(Duration.ZERO, origin, 2, 7,  eventProperties))
   .addAction(p.createPointerUp(0));

((RemoteWebDriver) driver).perform(Collections.singletonList(act));

因此,在本教程中,我們討論了使用 Selenium Webdriver 的筆事件。

廣告
© . All rights reserved.