You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Playwright中获取可见但不可交互文本框的文本?

Playwright 获取文本框文本的简便方法

针对文本框(包括 <input>、<textarea> 或可编辑元素),用Playwright获取内容有几种直接的方式:

  • 普通表单文本框(input/textarea):直接调用 inputValue() 方法,这是专门针对表单输入控件的方法,能直接拿到用户输入或预设的内容:

    // JavaScript 示例
    const textboxContent = await page.locator('#target-textbox').inputValue();
    
    # Python 示例
    textbox_content = page.locator("#target-textbox").input_value()
    
  • 富文本/可编辑元素(contenteditable):这类元素不属于标准表单控件,用 textContent() 或 innerText() 获取内容:

    const richText = await page.locator('#rich-text-editor').textContent();
    
    rich_text = page.locator("#rich-text-editor").text_content()
    
  • 排查报错要点:如果执行报错,先确认选择器是否精准定位到目标元素,必要时添加等待确保元素可见可交互:

    await page.locator('#target-textbox').waitFor({ state: 'visible' });
    const textboxContent = await page.locator('#target-textbox').inputValue();
    
    page.locator("#target-textbox").wait_for(state="visible")
    textbox_content = page.locator("#target-textbox").input_value()
    

内容的提问来源于stack exchange,提问作者Rahul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 04:14:59