【问题标题】:How to change value of input with same name using Puppeteer?如何使用 Puppeteer 更改同名输入的值?
【发布时间】:2020-01-01 14:00:12
【问题描述】:

所以有一种输入是这样的形式

<input class="input _ngcontent-EXCHANGE-32" focusableelement="" aria-invalid="false" aria-labelledby="930C5093-F4DD-4A01-BD2E-96F2873C8559--0" aria-disabled="false" tabindex="0" type="money64">

所以有两个像上面这样的输入。我想将第一个输入的值更改为 X,将第二个输入的值更改为 Y。

await page.$eval('input[type=money64]', el => el.value = 'X');
await page.$eval('input[type=money64]', el => el.value = 'Y');

上面的代码首先将相同输入的值更改为 X,然后更改为 Y,第二个输入保持不变。那么如何使用 puppeteer 设置第二个输入类型 = money64 的值。

【问题讨论】:

  • 你可以使用 :nth-child
  • 谢谢@AmanGupta,你能帮我写代码或写我可以使用的代码吗?我刚刚开始 node 和 puppeteer
  • 你能提供HTML结构吗?这些输入的父级是否还有其他子级?

标签: node.js automation automated-tests google-chrome-devtools puppeteer


【解决方案1】:
<div id="abc">
    <input class="custom1" type="text" >
    <input class="custom2" type="text">
</div>


 let firstInput = Array.from(document.querySelectorAll('#abc input:nth-child(1)'));
// change content for the firstInput
// repeat the same for the second child

试试这个。

你也可以试试:

await page.$eval('#abc input:nth-child(1)', el => el.value = 'X');
await page.$eval('#abc input:nth-child(2)', el => el.value = 'Y');

检查一下。

【讨论】:

  • 你能发布你的代码吗?你做了什么,你遇到了什么错误
  • 我没有收到错误,只是第二个输入没有更新
  • @vyom 在不知道您尝试过什么的情况下将无法提供帮助。请添加您的代码。
  • 只需要你最后的帮助,如果这是来自 chrome 的 xpath,我如何更改输入 /html/body/div[1]/root/div/div/div/div/child- 的值container-dart/div/exchange/unified-pricing/pricing-rule-list/pricing-rule-detail/form/drx-list-slidealog/drx-slidealog/focus-trap/div[2]/material-drawer/div /div[2]/drx-form-section[3]/div/div[2]/subrule-list-editor/div[2]/subrule-editor/subrule-cpm-thresholds-editor/div[2]/ cpm-threshold-editor/material-input/div[1]/div[1]/label/input
  • 我已经更新了我的答案。如果它不起作用,您需要使用正确的代码和示例 HTML 更新您的问题。否则没人能帮你。
猜你喜欢
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多