Puppeteer is not retrieving current in-page DOM element values

I’m trying puppeteer out.

Problem Description:

I made 2 functions, one for clicking a button and increase the item number to buy in cart. The second one is intended to retrieve the current in-page value that the button is supposed to change on click. My problem is that I can see the browser update values of a DOM element on clicking the button with puppeteer, but when retrieving it with button.evaluate(), it turns out to be the same as if I didn’t press it.

This problem is located in the first function (the second one is for additional context), where I retrieve two times the current value, one for the original number the DOM element had and the second for retrieving its value after click, which is expected to be 1 unit higher.

What I’ve tried so far:

  1. anElementHandle.evaluateHandle() just to make sure I retrieve an in-page input’s current value. But it seems to me I’m accessing a DOM snapshot.
  2. anElementHandle.evaluate() This was what I initially did.
  3. page.evaluate() and select the input manually.

Impressions:
My only guess is to refresh the page to force getting the new updated value of the input. But this seems to me clunky and an unnecessary workaround. I’d like to understand what’s happening under the hood for learning purposes.

Puppeteer version: 22.7.0

Node version: 20.12.2 LTS

The function in question:

async function increaseItemInCartByOne(plusButton: ElementHandle, shoppingCartAPI: any) {

try {
    const previousAmount = await shoppingCartAPI.cartItemAmountNumberByButton(plusButton)
    while (true) {
        const [_, response] = await Promise.all([
            plusButton.click(),
            page?.waitForResponse(r => r.request().url().includes('/cesta/') && r.request().method() === "PUT")
        ])

        if (response?.status() === 200) {
            const currentAmount = await shoppingCartAPI.cartItemAmountNumberByButton(plusButton)
            
            if (currentAmount === previousAmount) {
                const message = await getUIWarningMessage(4000) as string
                const outOfStock = message?.search(/lo sentimos no puede llevar la cantidad solicitada/i) >= 0
                if (outOfStock) {
                    throw new Error("This item is now out of stock")
                } else throw new Error("Don't know what's happening")
            } else if (currentAmount > previousAmount) break;
        }
    }

} catch (error) {
    console.log(error)
}
}

This is the function returning the object the main function consumes from:

async function shoppingCartAPI() {
//TODO collects the checkout items to buy in the shopping cart page and returns an API
//TODO This API is nothing but an object with methods having access to this function closure

await gotoCartMenu()
const productItemRows = await page?.$$(".table-row.prod-item-info")
if (!productItemRows) throw new Error("Shopping Cart is probably empty. Aborting")

return {
    cartItemAmountNumberByButton: async (button: ElementHandle) => {
        const updatedValue = await page?.evaluate((button) => button.previousElementSibling?.getAttribute("value")?.trim(), button);

        const newUpdatedValue = await page?.evaluate(() => {
            const newValue = document.querySelector(".table-row.prod-item-info:nth-child(2) .btn.cart-plus-btn")
            console.log(newValue)
            console.log(newValue?.previousElementSibling?.getAttribute("value"))
            return newValue?.previousElementSibling?.getAttribute("value")?.trim()
        });
            
        return Number(updatedValue)
    },
    getBuyAnotherItemInCartButtons: async () => {
        const buttons = []
        for (const row of productItemRows) {
            buttons.push(
                (await row.evaluateHandle((productRow) => {
                    const btn = productRow.querySelector(".btn.cart-plus-btn")
                    return btn
                })).asElement()
            )
        }

        if (buttons.length !== productItemRows.length)
            logger.error(`There are ${productItemRows.length} product(s) in cart and ${buttons.length} buttons for adding items are missing. This is weird`)

        return buttons
    }
}

}

Thank you for your time 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật