How to use variables in selector in Playwright C#?

1k Views Asked by At

So we are validating the text present in our site using the excel sheet.

for(int i = 0; i < services.Count; i++)
{
    string ServiceName = ExcelFunction.getCellValue(filepath, TestContext.CurrentContext.Test.MethodName, "ServiceName", i);
    ILocator service17 = page.Locator("//span[text()=ServiceName]");// here I want to append the ServiceName that I get from excel sheet to the selector.
}

Is there any way to append the variables in selectors in C#?

1

There are 1 best solutions below

0
Christian Baumann On BEST ANSWER

This should do it:

ILocator service17 = page.Locator("//span[text()=ServiceName" + ServiceName + "]");