首页 > 解决方案 > 使用 Playwright for Python,如何读取输入框的内容

问题描述

在我正在自动化的网站中,我有一个禁用的输入框。

在网站上,它显示一个数字,例如“1”,并具有其他元素来更改值。

<input data-v-a6368fd8="" id="base-amount-input-211" max="10" name="Counter" disabled="disabled" type="number" class="amount-input__number amount-input__number--active">

当我查询元素时

handle = page.querySelector('//input[starts-with(@name, "Counter")]')

我得到了句柄的结果。

当我尝试读取句柄内容时

handle.innerText() 

handle.innerHTML()

都是空的 ('')

如何访问当前输入值?

标签: pythonplaywrightplaywright-python

解决方案


另一种方法是简单地使用 的getAttribute()方法elementHandle来检索值:

handle = page.querySelector('//input[starts-with(@name, "Counter")]')
value = handle.getAttribute("value")

推荐阅读