【问题标题】:Select a dropdown item using xPath Option Index - Playwright (.net)使用 xPath 选项索引选择下拉项目 - Playwright (.net)
【发布时间】:2021-11-26 19:24:26
【问题描述】:

我正在尝试使用“ClickAsync”选择一个下拉元素,因为用户模拟点击以使用 xPath 在下拉列表中选择一个项目。但不幸的是,我无法执行此操作。虽然我可以使用点击展开下拉菜单,但无法选择可用选项。

        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);

【问题讨论】:

  • 这是原生 HTML 选择元素吗?
  • 是的。 HTML 选择。

标签: c# .net playwright playwright-sharp


【解决方案1】:

您应该改用SelectOptionAsync 方法。

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

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 2021-11-15
    • 2020-09-15
    • 1970-01-01
    • 2020-05-15
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多