Mark
Mark

Reputation: 35

How do I find the exact row of a table using Playwright & C# when there is similar text

Using this playwright code:

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

works if there is only one value of "PWATest_20240430_100831".

However, if you add another row with the prefix of "No2Sch" + "PWATest_20240430_100831" then when running the code, I am getting an error:

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

📝 "CustomerName" will be the same.

I have tried all sorts of approaches and still cannot fathom how I can specify exact text. You can't use it in the .filter, unless you are using something other than C# as it doesn't recognise Exact = true.

Has anyone else had a similar issue and if so, how did you get your code to work please?

📝 If it helps, here is the html for the row:

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

Upvotes: 0

Views: 839

Answers (1)

EasyReproLearner
EasyReproLearner

Reputation: 19

The first and foremost thing you could try is change "HasText" to "HasTextRegex". More examples are described here.

Upvotes: 1

Related Questions