0

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 | });
JaSON
  • 4,843
  • 2
  • 8
  • 15
Sarah G
  • 21
  • 10

1 Answers1

0

I believe that if you want to pass an XPath expression as a parameter to the locator method in Playwright then you should include the prefix xpath=

https://playwright.dev/docs/other-locators#xpath-locator

Conal Tuohy
  • 2,561
  • 1
  • 8
  • 15