1

I have a span tag where we have a value 2 which keeps changing. I want to store this value in a variable either using javascript or playwright (a testing tool).

<span class="dynamic classname" data-testid="typography">2</span>

Note: Even class name is not constant.

const count = document.getElementByTagName('span');

which obviously doesn't work.

Using playwright:

const element = await page.locator('[data-testid="typography"]');
const count = await element.getAttraibute(element);

I'm new to javascript and playwright, any lead would be helpful.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • If the target element has no unique characteristics to select by, then target it by using the parent HTML structure, eg. `document.querySelector('#container #nav .foo-container > span').dataset.testid` – Rory McCrossan Jan 19 '23 at 15:49
  • You have a typo in function name. It should be document.getElementsByTagName('span') – Grzegorz Adam Kowalski Jan 19 '23 at 15:50
  • 2
    @GrzegorzAdamKowalski also getAttraibute, I don't know `page.locator` selector support, but `document.querySelector('[data-testid="typography"]')` will work – Lawrence Cherone Jan 19 '23 at 15:52

1 Answers1

0

Javascript

const count = document.getElementsByTagName('span')[0].innerText;

more examples

Playwright

const element = await page.locator('[data-testid="typography"]');
const count = await element.innerText();

see innerText

user2245191
  • 61
  • 1
  • 6