如何提交表格? 我有一个简单的搜索栏和一个搜索按钮。以下内容输入搜索字符串,但点击事件未被触发。当无头被设置为假,并且我手工点击进入serachfield时,搜索正在工作。如何提交按钮?木偶 - 如何提交表格
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>);
}
UPDATE
这不是工作:
const searchForm = await page.$('#search-form');
await searchForm.evaluate(searchForm => searchForm.submit());
它不回发,并在表单中的文本被清除,即使它sh用preventDefault触发该功能。
所以这个问题似乎是这行代码伺机
searchForm.evaluate(searchForm => searchForm.submit());
这是工作:
我改变了代码:
await page.click('#search-button');
await page.waitForNavigation();
截图看看怎么样,如果这个问题没有解决。请考虑附加它。 –