menu

Questions & Answers

How to print text after specific div class (msg char-msg) in Playwright?

This script just give an error ref: <Node>

from playwright.sync_api import Playwright, sync_playwright, expect
import time


def run(playwright: Playwright) -> None:
    browser = playwright.firefox.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://url")
    page.get_by_role("button", name="I understand").click()
    page.get_by_placeholder("Type a message").fill("hello")
    page.locator("form").get_by_role("button").first.click()
    time.sleep(10) #Waiting, because the output text is still rendering
    output_text = page.query_selector('.char-msg').json_value()
    print(output_text)
    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

How do i find the specific div class and print the text while it's still rendering/loading?

The output text located in

<div class="msg char-msg"><div style="max-width: 100%;"><div class="markdown-wrapper markdown-wrapper-last-msg swiper-no-swiping"><div style="overflow-wrap: break-word;"><p>This is the output text</p></div></div></div><div></div></div>

I can't find the output text after specific div class, it should give me an output while the text is rendering, the output just give me an error ref: <Node>

Comments:
2023-01-25 00:30:04
where is the error thrown?
2023-01-25 00:30:04
@bb4L forgive me, english is not my native language. it's not an error. but it's really just print ref: <Node> from output_text = page.query_selector('.char-msg').json_value() print(output_text)
2023-01-25 00:30:04
no worries, have you checked the content of the page (without selecting something special)?
Answers(1) :

Fixed by changing from this

    output_text = page.query_selector('.char-msg').json_value()
    print(output_text)

to this

    div = page.query_selector('div.msg.char-msg')
    output_text = div.inner_text()
    print(output_text)