Q:

puppeteer : set a dropdown option as selected based on text in an external function

Because I dont find puppeteer native function for set a dropdown option as selected based on text and not on value, I am tryiong to write it by myself
async function setSelectName(page, selector, valText) {
   return await page.evaluate((data) => {
       console.log(JSON.stringify(data));

       let options = document.querySelectorAll(data.selector);
       console.log(JSON.stringify(options));

       let optionCount = options.length;

       for (let i = 0; i < optionCount; ++i) {
           if (options[i].text === data.valText) {
               console.log('match');
                document.querySelector(data.selector).value = options[i].value;
           }
       }

   }, {selector, valText})
}

Unfortunately it does not work, and when i try to display selected options in console it displays me:

{"0":{"0":{},"1":{},"2":{},"3":{}}}

Do understand what happens and how to do it?

A:

async function selectOptionByText(page, seletctId, textWanted) {

   const optionWaned = (await page.$x(`//*[@id = "${seletctId}"]/option[text() = "${textWanted}"]`))[0];

   const opionValue = await (await optionWaned .getProperty('value')).jsonValue();

   await page.select(`#${seletctId}`', opionValue );
}

