In Puppeteer, we can select an option of a dropdown by providing the value as a parameter:
page.select('select#idOfSelect', 'optionValue');
Is there a function to select an option based on its text and not from its value?
You can use page.evaluate()
to find
the option
you would like to select by its text
property. Then you can use the selected
property to indicate that the option is currently selected:
await page.evaluate(() => {
const example = document.querySelector('#example');
const example_options = example.querySelectorAll('option');
const selected_option = [...example_options].find(option => option.text === 'Hello, world!');
selected_option.selected = true;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With