I am trying to select an option dynamically using puppeteer but I'm running into some problems on how to get it correctly. I need to get the actual value associated with the option tag. This is making my problem a bit harder. I searched a little bit and found that some people are using Xpath to make the problem a little easier to get, but it seems that my Xpath is not working correctly, as I'm getting an error ->
The string '//select[@id = "wrestler"]/option[text() = Henri Mugnier]' is not a valid XPath expression.
Henri Mugnier is at index 0 in my holderArr array.
Here is my small code snippet.
const option = (await frame.$x(
`//select[@id = "wrestler"]/option[text() = ${holderArr[i].name}]`
))[0];
holderArr is getting passed in from another function, it's an array of objects. The dropdown is a list of names, but the value for each of them is just random numbers, So I'm hoping that using an xpath and then grabbing the value with a function like this
const value = await (await option.getProperty('value')).jsonValue();
Steps to reproduce:
This is how I ended up getting it to work. This will give you the value property on the option tag using puppeteer.
let [optElementHandle] = await frame.$x(`//select[@id="wrestler"]//option[contains(text() , "${holderArr[i].name}")]`)
const text = await optElementHandle.getProperty('value')
const value = await text.jsonValue()