Step by step tutorial on Puppeteer :: Part-3

Data scrapping from HTML table

We will scrap data from the following page:
https://demoqa.com/webtables

Write following code:

it("Web data scrapping", async () => {
        await page.goto("https://demoqa.com/webtables");
        let result = await page.evaluate(() => {
            let rows = document.querySelectorAll('.rt-tbody');
            return Array.from(rows, row => {
                let columns = row.querySelectorAll('.rt-td');
                return Array.from(columns, column => column.innerText);
            });
        });
        console.log(result[0]);
        let array = result[0];
        for (let i = 0; i < array.length; i++) {
            console.log(array[i]);
        }
    })

Output:

Cierra
Vega
39
cierra@example.com
10000
Insurance

Alden
Cantrell
45
alden@example.com
12000
Compliance

Kierra
Gentry
29
kierra@example.com
2000
Legal

Handling Multiple Tab and Windows

You can handle multiple tab or window. Here is the code:

it("Browser tab handling",async()=>{
        await page.goto("https://demoqa.com/browser-windows");
        await page.click("#tabButton");
        await page.waitForTimeout(3000)
        let tabs = await browser.pages(); // get all tab
        let currentTab = tabs[tabs.length - 1]; // get current tab
        console.log(currentTab.url()); // get current tab url

        let header = await currentTab.$("#sampleHeading")
        let text = await currentTab.evaluate(e => e.textContent, header);
        console.log(text);
        await currentTab.close();
    })
    it("Browser window handling",async()=>{
        await page.goto("https://demoqa.com/browser-windows");
        await page.click("#windowButton");
        await page.waitForTimeout(3000)
        let tabs = await browser.pages(); // get all tab
        let currentTab = tabs[tabs.length - 1]; // get current tab
        console.log(currentTab.url()); // get current tab url

        let headerElement = await currentTab.$("#sampleHeading")
        let headerText = await currentTab.evaluate(e => e.textContent, headerElement);
        console.log(headerText);
        await currentTab.close();
    })

Handling Alert

it("Handling alert", async () => {
        await page.goto("https://demoqa.com/alerts");
        let alertButtonSelector = "#alertButton";
        await page.waitForSelector(alertButtonSelector);
        await page.on('dialog', async (dialog) => {
            await dialog.accept();
        });
        let buttonElement = await page.$$(alertButtonSelector);
        await buttonElement[0].click();
        await page.waitForTimeout(3000);
    
    })

Handling IFrame

it("Handling IFrame", async () => {
        await page.goto("https://demoqa.com/frames");
        let iframeSelector = "#frame1";
        await page.waitForSelector(iframeSelector);
        let frameHandle = await page.$(iframeSelector);
        let frame = await frameHandle.contentFrame();
        let headerElement = await frame.$('#sampleHeading');
        let headerText = await frame.evaluate(e => e.textContent, headerElement);
        console.log(headerText);

    })

Accessing Keyboard

Keyboard press enter:
await page.keyboard.press('Enter');
Keyboard press backspace:
await page.keyboard.press('Backspace');
Keyboard press Arrow Down:
await page.keyboard.press('ArrowDown'); //same for ArrowLeft, ArrowRight, and ArrowUp

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *