当存在类似文本时,如何使用 Playwright 和 C# 找到表格的确切行

问题描述 投票:0回答:1
  • 使用用C#编写的Playwright
  • 我有一个包含各种列和行的表格。
  • 每一行都有一个唯一的名称,同一行上有一些 按钮。
  • 我只对按钮集中的删除/删除按钮感兴趣。

使用此剧作家代码:

        public static async Task ClickScenarioDeleteButton(IPage Page, string scenarioName)
        {
            //var deleteButton = Page.Locator($"div:has-text('{scenarioName}') >> data-testid=generic-remove-button")
            //    .First;
            //await deleteButton.ClickAsync();

                await Page
                    .GetByRole(AriaRole.Row)
                    .Filter(new() { HasText = scenarioName })
                    .GetByTestId("generic-remove-button")
                    .ClickAsync();
            
        }

如果“PWATest_20240430_100831”只有一个值,则有效。

但是,如果您添加另一行前缀为“No2Sch”+“PWATest_20240430_100831”,那么在运行代码时,我会收到错误:

Message: 
  Microsoft.Playwright.PlaywrightException : Error: strict mode violation: GetByRole(AriaRole.Row).Filter(new() { HasText = "PWATest_20240430_100831" }).GetByTestId("generic-remove-button") resolved to 2 elements:
      1) <button type="button" data-testid="generic-remove-button…>…</button> aka GetByRole(AriaRole.Row, new() { Name = "CustomerName PWATest_20240430_100831 24 Hours None" }).GetByTestId("generic-remove-button")
      2) <button type="button" data-testid="generic-remove-button…>…</button> aka GetByRole(AriaRole.Row, new() { Name = "CustomerName No2SchPWATest_20240430_100831 24 Hours None" }).GetByTestId("generic-remove-button")

📝 “客户名称”将是相同的。

我尝试了各种方法,但仍然无法理解如何指定确切的文本。 您不能在 .filter 中使用它,除非您使用的是 C# 以外的其他语言,因为它无法识别

Exact = true

还有其他人遇到类似的问题吗?如果有,你是如何让你的代码工作的?

📝 如果有帮助,这里是该行的 html:

<td data-label="Valid" class="mud-table-cell"><div class="scenario-validity" b-ho1i1ctrb5=""><!--!--><div class="icon-component" b-ehxpcnsxe0=""><!--!--><div class="mud-tooltip-root mud-tooltip-inline"><!--!--><svg class="mud-icon-root mud-svg-icon mud-success-text mud-icon-size-small" focusable="false" viewBox="0 0 24 24" aria-hidden="true"><!--!--><path d="M0 0h24v24H0z" fill="none"></path><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"></path></svg><!--!--><div id="popover-1f0d4cb7-5b9a-4b7a-80c5-54be38c1bcf0" class="mud-popover-cascading-value"></div></div></div></div></td>
c# html-table selector playwright
1个回答
0
投票

您可以尝试的第一个也是最重要的事情是将“HasText”更改为“HasTextRegex”。 此处描述了更多示例。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.