如何使用 playwright 单击元素的特定位置?

问题描述 投票:0回答:1

我正在从动态网站中抓取数据,因此需要执行一些操作才能将数据加载到 DOM 中。有一个水平滚动条需要单击,到目前为止,我可以执行此操作。但是,如果我可以模拟单击特定位置(例如该元素的右侧)的操作,那就更有趣了。有办法做到这一点吗?

    const iframe = page.frameLocator('iframe[allowfullscreen]');
    await iframe.locator('div.tableEx > div:nth-child(7)').click();

我尝试的内容按预期工作,现在我想将此单击定向到选择器中的特定位置。

typescript web-scraping playwright
1个回答
0
投票

您可以获取元素的

boundingBox
,然后使用
mouse
属性单击基于元素边界框的任意坐标。

test('Click at position', async ({ context, page }) => {
  await page.goto("https://www.mabl.com");
  var element = page.locator('"Login"');
  var box = (await element.boundingBox())!;
  await page.mouse.click(box.x + box.width / 2, box.y + box.height - 5);
});
© www.soinside.com 2019 - 2024. All rights reserved.