Search code examples
c#playwrightplaywright-sharpplaywright-dotnet

How to use variables in selector in Playwright C#?


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


Solution

  • This should do it:

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