Selenium 简明教程

Selenium WebDriver - Pen Events

Selenium Webdriver 可用于执行类似于笔移动的指针输入,以与页面上的 Web 元素进行通信。这是使用 * Actions Class* 执行的。笔事件仅在 Chromium 浏览器中可用。

What is a Pen Action?

笔类似于具有类似于鼠标特征的指针输入。它还可以具有不同于手写的事件属性。鼠标由五个按钮组成,而笔只能使用三个按钮,即 0 对应于类似于左键单击的触摸接触,2 对应于类似于右键单击的钢笔按钮,5 对应于橡皮擦按钮(驱动程序尚不支持此功能)。

Syntax to Perform a Pen Event

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();

Syntax to Perform Pointer Event Attributes

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 执行的笔事件。