Employee Name
Sam
Employee Name
Sam
Employee Name
Sam

C# PlayWright - Getting the value of element /dt and /dd inside div

207 Views Asked by At

I have below structure.:

<dl class="er-ctrbs-review-totals-list" </dl>
   <div data-qa="total-1">
      <dt>Employee Name</dt>
      <dd>Sam</dd>
   </div>
   <div data-qa="total-2">
      <dt>Employer Name</dt>
     <dd>ZenS</dd>
   </div>
</dl>

I am trying to get the values of dt and dl. I tried with: page.locator woth [data-qa] attribute but its returning locator only not value.

Then I have tried :

_page.Locator("xpath=//div[@data-qa='total-1']/dt");
_page.Locator("xpath=//div[@data-qa='total-2']/dt");

also with this change:

_page.Locator("xpath=*//div[@data-qa='total-1']/dt");
_page.Locator("xpath=*//div[@data-qa='total-2']/dt");

Can anyone please help me how can I get the label and value under div? I am trying to get the: Employee Name, Sam, Employer Name, ZenS

1

There are 1 best solutions below

2
Hossein Sabziani On

You can use the regeX and take the values of <dt> and <dd>

var input = File.ReadAllText("data.txt");
string patternDD = "<dd.*?>(.*?)<\\/dd>";
string patternDT = "<dt.*?>(.*?)<\\/dt>";

var matchesDD = Regex.Matches(input, patternDD);
foreach (Match m in matchesDD)
    Console.WriteLine(m.Value.Replace("<dd>", "").Replace("</dd>", ""));
//Sam - ZenS

var matchesDT = Regex.Matches(input, patternDT);
foreach (Match m in matchesDT)
    Console.WriteLine(m.Value.Replace("<dt>", "").Replace("</dt>", ""));
//Employee Name - Employer Name