Here's an example of a Playwright TypeScript Page Object Model (POM) function that retrieves the text below the <b>Date started:</b> element:
import { Page } from 'playwright';
export class ExamplePage {
private page: Page;
constructor(page: Page) {
this.page = page;
}
async getDateStartedText(): Promise<string> {
const dateStartedElement = await this.page.$('div.col b:has-text("Date started:")');
const nextSibling = await dateStartedElement?.evaluateHandle((node) => node.nextSibling);
const dateStartedTextHandle = await (await nextSibling?.asElement())?.getProperty('textContent');
const dateStartedText = await dateStartedTextHandle?.jsonValue();
return dateStartedText || '';
}
}
You can use this function to extract the text from the provided HTML:
import { chromium } from 'playwright';
import { ExamplePage } from './ExamplePage';
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.setContent(`
<div class="row">
<div class="col">
<b>Date requested:</b>
24-Oct-23 14:16:00 (UTC +03:00)
<br>
<b>Date started:</b>
24-Oct-23 15:22:01 (UTC +03:00)
<br>
b>Date completed:</b>
-
<br>
<br>
<b>Date last updated:</b>
24-Oct-23 15:22:01 (UTC +03:00)
<br>
<b>Last updated by:</b>
<a class="employee-popover" data-bs-content="<small><i class='fas fa-key'></i> ergomed\se-testuser-03<br /><i class='fas fa-envelope'></i> <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS><br /><i class='fas fa-users'></i> IT (Group), Test<br /> <i class='fas fa-map-marker-alt'></i> UK (Home Based)<br /></small>" data-bs-trigger="hover">SE User 03 Test</a>
<br>
<br>
<b>Language from:</b>
German (DE)
<br>
<b>Language to:</b>
English (EN)
<br>
<br>
<a href="/4/attachments/41302/download"><svg class="svg-inline--fa fa-download" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="download" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" data-fa-i2svg=""><path fill="currentColor" d="M480 352h-133.5l-45.25 45.25C289.2 409.3 273.1 416 256 416s-33.16-6.656-45.25-18.75L165.5 352H32c-17.67 0-32 14.33-32 32v96c0 17.67 14.33 32 32 32h448c17.67 0 32-14.33 32-32v-96C512 366.3 497.7 352 480 352zM432 456c-13.2 0-24-10.8-24-24c0-13.2 10.8-24 24-24s24 10.8 24 24C456 445.2 445.2 456 432 456zM233.4 374.6C239.6 380.9 247.8 384 256 384s16.38-3.125 22.62-9.375l128-128c12.49-12.5 12.49-32.75 0-45.25c-12.5-12.5-32.76-12.5-45.25 0L288 274.8V32c0-17.67-14.33-32-32-32C238.3 0 224 14.33 224 32v242.8L150.6 201.4c-12.49-12.5-32.75-12.5-45.25 0c-12.49 12.5-12.49 32.75 0 45.25L233.4 374.6z"></path></svg><!-- <i class="fas fa-download"></i> Font Awesome fontawesome.com --> Download source file</a>
</div>
</div>
`);
const examplePage = new ExamplePage(page);
const dateStartedText = await examplePage.getDateStartedText();
console.log(dateStartedText);
await browser.close();
})();
Ensure that you replace the HTML content in page.setContent with your actual HTML or provide the URL to the page using await page.goto. The script will output the text below the <b>Date started:</b> element in the console.