Home > Enterprise >  Select a dropdown item using xPath Option Index - Playwright (.net)
Select a dropdown item using xPath Option Index - Playwright (.net)

Time:10-07

I am trying to select a dropdown element using "ClickAsync" as user simulate Clicks to select a item on the dropdown using xPath. But unfortunately I am unable to perform this. Although I can expand the drop down using the click, unable to select an option available.

        await page.GotoAsync("https://letcode.in/dropdowns");
        await page.WaitForTimeoutAsync(3000);
        //await page.ClickAsync("xpath=//select[@id='fruits']/option[3]");
        
        await page.ClickAsync("xpath=//select[@id='fruits']");// this will expand the dropdown
        //await page.ClickAsync("xpath=//option[3]");
        await page.WaitForTimeoutAsync(3000);

CodePudding user response:

You should use the SelectOptionAsync method instead.

await page.SelectOptionAsync(
    "xpath=//select[@id='fruits']", 
    new SelectOptionValue { Index = 2 }));
  • Related