如何提交表格?
我有一个简单的搜索栏和一个搜索按钮。以下输入搜索字符串,但不会触发click事件。当headless设置为false并且我手动在serachfield中单击Enter时,搜索正在工作。如何提交按钮?

  const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto('http://localhost:3000', {waitUntil: 'networkidle'});

await page.focus('#search');

// Type our query into the search bar
await page.type('michael');

// Submit form
await page.press('Enter');

await page.screenshot({path: './screenshots/search3.png'});

browser.close();
})();

我的搜寻列
search(e) {
 e.preventDefault();
this.props.onClick(this.props.value);}

render() {
return (<form className="form-inline" id="search-form" onSubmit
{this.search}>
  <div className="form-group">
    <input
      type="text"
      className="form-control"
      id="search"
      value={this.props.value}
      placeholder={this.props.placeHolder}
      onChange={this.props.onChange}
    />

    <button primary type="submit" >
      {this.props.buttonText}
    </button>
  </div>
</form>);
}

更新

这不起作用:
const searchForm = await page.$('#search-form');
await searchForm.evaluate(searchForm => searchForm.submit());

它执行回发操作,并且清除了表单中的文本,即使它应使用preventDefault触发函数。

所以问题似乎是等待这行代码
searchForm.evaluate(searchForm => searchForm.submit());

,这是可行的:

我将代码更改为:
await page.click('#search-button');
await page.waitForNavigation();

最佳答案

我通过使用此方法而不是提交表单来解决它:

await page.click('#search-button');
await page.waitForNavigation();

07-24 09:44
查看更多