How do I submit the form? I have a simple search bar and a search button. The following enters the search string, but the click event is not fired. When headless is set to false and I manully click enter in the serachfield, the search is working. How to submit the button?
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();
})();
My searchbar
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
This is not working:
const searchForm = await page.$('#search-form');
await searchForm.evaluate(searchForm => searchForm.submit());
It does a postback and the text in the form was cleared, even though it should trigger the function with preventDefault.
So the issue seems to be this line of code await
searchForm.evaluate(searchForm => searchForm.submit());
This is working:
I changed the code to:
await page.click('#search-button');
await page.waitForNavigation();