typescript - 剧作家在有头模式下工作,但在 headless 模式下失败

标签 typescript web-scraping playwright opensea

我正在尝试使用此示例来获取 NFT 在 opensea 中的报价数量:

import { test, expect } from '@playwright/test';

test('test', async ({ page }) => {
    await page.goto('https://opensea.io/assets/ethereum/0x63217dbb73e7a02c1d30f486e899ee66d0aa5e0b/6341');
    await page.waitForLoadState('networkidle');

    let selector = page.locator("[id='Body offers-panel'] li");
    const offers = await selector.count();

    console.log('Num of offers:', offers);
});

然后我运行“npx playwright test”,总是打印“Num of Offers: 0”

但是如果我在 --headed 模式下运行它,它会完美地工作并输出“Num of Offers: 5”

谁能解释/帮助我理解它?

我尝试使用:

let selector = page.locator("[id='Body offers-panel'] li").waitFor();

尝试等待所有请求完成

await page.waitForLoadState('networkidle');

尝试等待选择器:

let selector = page.locator("[id='Body offers-panel'] li").first().waitFor();

但是没有一个起作用,除非我在 --headed 模式下运行测试,否则无论我尝试哪个 NFT 地址,我的计数总是 0。

我想解决它或了解为什么会发生这种情况

最佳答案

某些网站如果检测到 headless 客户端,将不会加载页面。这是为了防止抓取等。我猜这就是这里发生的事情

参见:
Are you headless?
Detect Headless

关于typescript - 剧作家在有头模式下工作,但在 headless 模式下失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75488727/

相关文章:

javascript - 如何等待子组件任务完成后再继续

javascript - 在 Python 中使用 Playwright 选择特定 Web 元素时出现问题

python - 如果我想做这个,我该从哪里开始?

python - 如何在 Python 中使用 Beautifulsoup 查找 div 内的所有 anchor 标签

Python程序随机挂起

python - Python Playwright 中的错误:playwright._impl._api_types.Error: net::ERR_ABORTED at ...等待直到 "load"错误 Python Playwright

Playwright "setup"项目未在依赖项目之前执行

html - 添加 formControlName 会使默认值在我的 Angular 表单下拉列表中消失

typescript - Webpack watch 不会在第二次更改时生成输出

angular - 使用自定义类中的 Angular MatDialog(不包含在 app.module.ts 中)