Skip to content

how to handle redirect url in js code doesnot work if site uses redirect #2336

@Johnydeo

Description

@Johnydeo

import { lightpanda } from '@lightpanda/browser';
import { chromium } from 'playwright-core';

const lpdopts = {
host: '127.0.0.1',
port: 9222,
};

(async () => {
const proc = await lightpanda.serve(lpdopts);
const browser = await chromium.connectOverCDP(ws://${lpdopts.host}:${lpdopts.port});
const context = await browser.newContext();
const page = await context.newPage();

try {
await page.goto("https://anysite", { timeout: 30000 });
await page.waitForURL("**", { waitUntil: 'domcontentloaded', timeout: 15000 });

// Extra safety — wait for first <p> to exist
await page.locator('p').first().waitFor({ state: 'attached', timeout: 15000 });

const p_e = await page.locator('p').evaluateAll(ps =>
  ps.map(p => p.textContent.trim()).filter(t => t.length > 0)
);

console.log("Total <p> tags found:", p_e.length);
console.log("First:", p_e[0] ?? "not found");
console.log("Second:", p_e[1] ?? "not found");

} catch (err) {
console.error("Error during scraping:", err.message);
} finally {
await page.close();
await context.close();
await browser.close();
proc.stdout.destroy();
proc.stderr.destroy();
proc.kill();
}
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions