html 如何在Playwright中打印特定div类(msg char-msg)后的文本?

lg40wkob  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(160)

此脚本仅给予错误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)

如何找到特定的div类,并在渲染/加载时打印文本?
位于中的输出文本

<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>

我找不到特定div类之后的输出文本,在呈现文本时,它应该会给予我一个输出,但输出只给我一个错误ref: <Node>

fcwjkofz

fcwjkofz1#

通过更改此

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

到这个

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

相关问题