作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 50 个输入字段的页面,我正在尝试更改使用 Puppeteer 的值。
不幸的是,只是设置如下值不起作用,因为网站没有检测到该字段的更改:
await page.evaluate(() => {
document.querySelectorAll('input[name="price"]').forEach(function(item) {
item.value = "14,50";
});
});
所以我发现我必须使用
focus()
和
keyboard.type()
.我遇到的问题是输入字段没有唯一的名称,而且我不知道如何写
focus()
循环输入的选择器。
<input _ngcontent-lhe-c73="" id="id" type="text" name="price" autocomplete="off" class="price-input" placeholder="vul de prijs in">
const inputs = await page.$eval('.price-input');
for (let i = 0; i < inputs.length; i++) {
await page.evaluate(() => { document.querySelectorAll('input[name="price"]')[i].value = ''; });
await page.focus('input[name="price"]['+i+']'); // this is invalid
await page.keyboard.type('14,50');
}
我该如何写
focus()
选择器在循环中选择正确的值?我一直在尝试 nth-child 但这似乎不可能。
最佳答案
你可以尝试这样的事情:
const inputs = await page.$$('.price-input');
for (const input of inputs) {
await page.evaluate((element) => { element.value = ''; }, input);
await input.focus(); // May be redundant as we use the element for typing below.
await input.type('14,50');
}
关于javascript - Puppeteer 焦点循环遍历输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64589340/
我是一名优秀的程序员,十分优秀!