首页 > 解决方案 > How to select a drop down menu option in Puppeteer bot?

问题描述

I am trying to customise a Puppeteer bot which fills out a reservation form, searches for an available date and if it’s not available it refreshes the page. The bot only selects some options but I’d like to add more. Currently it leaves the default “1” room in the drop down menu but I’d like to select “2”. Below is the current code and I’d like to select 2 for no_of_rooms

async function prepareAndCheckPage(){
    //select 2 rooms
    await page.select('#form_no_of_rooms', '2'); //this code doesn't work
    //accessibility requirement – this code correctly selects the second radio button
    await page.waitForSelector('#form_rooms_0_accessibilityRequirement_1');
    const accessibilityRequirement = false; 
    page.$eval('#form_rooms_0_accessibilityRequirement_' + (accessibilityRequirement ? 0 : 1), elem => {
        elem.checked = true;
    });

Here is the HTML from the page

  <div id="accommodation-add-rooms">
    <div class="row / mb-6">
      <div class="col-md-7">
      <div class="form-group mb-0"><label class="required" for="form_no_of_rooms">Preferred number of rooms*</label><div id="form_no_of_rooms_help" class="d-block / mb-1">Please note your preferred number of rooms and room configuration are not guaranteed.</div><select id="form_no_of_rooms" name="form[no_of_rooms]" class="col-12  col-md-2 form-control" aria-describedby="form_no_of_rooms_help"><option value="1">
    1
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  </option><option value="2">
    2
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  </option></select>

Obviously I'm very inexperienced in this. What am I doing wrong? I’ve tried both form_no_of_rooms and no_of_rooms

标签: puppeteer

解决方案


推荐阅读