1

In Playwright I created a script to verify the title using toHaveAttribute assertion for this element:

<title style="" xpath="1">Unient | Your Versatile Partner for Better Offshoring</title>

Here is my code:

    import { test, expect } from '@playwright/test';

test('Landing page', async ({ page }) => {
  await page.goto('https://www.unient.biz/');
  await page.getByRole('button', { name: 'Accept All' }).click();
  const Title = page.locator("/html/head/title[@xpath='1']");
  await expect(Title).toHaveAttribute("title","Unient | Your Versatile Partner for Better Offshoring");
});

But there is an error and I am not really sure how to fix this:

expect.toHaveAttribute: Unsupported token "@xpath" while parsing selector "/html/head/title[@xpath='1']"
=========================== logs ===========================
expect.toHaveAttribute with timeout 5000ms
============================================================

  5 |   await page.getByRole('button', { name: 'Accept All' }).click();
  6 |   const Title = page.locator("/html/head/title[@xpath='1']");
> 7 |   await expect(Title).toHaveAttribute("xpath","Unient | Your Versatile Partner for Better Offshoring");
    |                       ^
  8 | });

2 Answers 2

1

You should use ideally toHaveTitle

await expect(page).toHaveTitle(/.*titleTextORregularExpression/);

Reference: https://playwright.dev/docs/api/class-pageassertions#page-assertions-to-have-title

0

The error message you received indicates that the selector you provided is not valid due to the unsupported attribute @xpath. Instead of using @xpath, you can use a more generic selector for the title element using the textContent property, like this:

const Title = page.locator("title");
await expect(Title).toHaveAttribute("textContent","Unient | Your Versatile Partner for Better Offshoring");

This code should correctly locate the title element on the page and verify its text content using the toHaveAttribute assertion.

Here's the updated script:

import { test, expect } from '@playwright/test';

test('Landing page', async ({ page }) => {
  await page.goto('https://www.unient.biz/');
  await page.getByRole('button', { name: 'Accept All' }).click();
  const Title = page.locator("title");
  await expect(Title).toHaveAttribute("textContent","Unient | Your Versatile Partner for Better Offshoring");
});

For more information on the toHaveAttribute assertion in Playwright, you can refer to the official documentation: https://playwright.dev/docs/api/class-elementhandle#elementhandletohaveattributeattribute-value-options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.