在Playwright中拖动元素

huangapple go评论112阅读模式
英文:

Drag element on Playwright

问题

我想知道是否可以使用Golang Playwright点击并拖动网页元素,因为它存在点击和移动,但没有同时操作两者的方法。

英文:

I would like to know if it is possible click and drag an element of web page with Golang Playwright, because it exists click and move, but not both joints.

答案1

得分: 2

我得到了一个可以拖动Playwright中元素的工作版本。以下是TypeScript的实现代码:

async dragElement(element: ElementHandle, xOffset: number, yOffset: number, page: Page): Promise<void> {
  const elementBox = await element.boundingBox();
  if (!elementBox) {
    throw new Error('无法找到元素的边界框');
  }

  const elementCenterX = elementBox.x + elementBox.width / 2;
  const elementCenterY = elementBox.y + elementBox.height / 2;

  await element.hover();
  await page.mouse.down();
  await page.mouse.move(elementCenterX + xOffset, elementCenterY + yOffset);
  await page.mouse.up();
}

请注意,这只是一个翻译,我无法运行代码或提供其他帮助。

英文:

I got a working version that drags an element in Playwright. This is the implementation in TypeScript.

async dragElement(element: ElementHandle, xOffset: number, yOffset: number, page: Page): Promise&lt;void&gt; {
  const elementBox = await element.boundingBox();
  if (!elementBox) {
    throw new Error(&#39;Unable to find bounding box on element&#39;);
  }

  const elementCenterX = elementBox.x + elementBox.width / 2;
  const elementCenterY = elementBox.y + elementBox.height / 2;

  await element.hover();
  await page.mouse.down();
  await page.mouse.move(elementCenterX + xOffset, elementCenterY + yOffset);
  await page.mouse.up();
}

huangapple
  • 本文由 发表于 2021年7月15日 17:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/68391706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定