【发布时间】:2018-04-02 16:31:16
【问题描述】:
在 puppeteer 中,我正在尝试提交表单,在填写基本详细信息后,大多数情况下我单击提交按钮就足够了,但有时这不起作用,我必须再次单击按钮才能继续下一页。有什么办法可以在我的程序中实现它吗?
我认为这可以通过在点击后等待一个事件来实现,如果该事件没有发生,则再次启动点击,但我什至不知道该等待哪个。很抱歉,这可能是一个菜鸟问题,但我是 JavaScript 新手,所以我对这些事件了解不多。
await this.page.goto('https://www.urlwithform.com', {waitUntil: 'load'});
await genericParser.enterDetailsLabel(this.page,this.customerDetails);
await this.page.type('#txtCountry', this.depCity);
await this.page.evaluate((depCity) => {
var citySuggestions = document.querySelectorAll('div.autocomplete-suggestion');
var newDepCity = depCity.toUpperCase();
var cityRegex = new RegExp(newDepCity);
citySuggestions.forEach((city) => {
if(city.textContent.match(cityRegex)) {
city.click();
}
});
},this.depCity);
await this.page.waitFor(1000);
await this.page.waitFor('button');
await this.page.click('#MainBody_IWOVID_item_1_btnCheckIn');
enterDetailsLabel 输入表单详情,page.evaluate 里面的代码是更多特定于表单中选择城市的代码。
【问题讨论】:
-
如果您不发布代码,我们将无法帮助您。
-
“但有时这不起作用” - 这是您需要解决的问题
-
@jmargolisvt 更新了我的代码,请交叉检查。
-
@Igor 我知道要修复什么,我不知道如何修复。
-
使用
await this.page.waitFor(1000);始终是代码损坏的标志。您应该摆脱它并找到另一种方法来检测所需的程序状态。
标签: javascript node.js puppeteer