Skip to content

[Bug]: locators suggested are incorrect when using getByText and the text contains digits #31832

Open
@jenvareto

Description

@jenvareto

Version

1.45.3

Steps to reproduce

  1. Create an HTML file with this body:
        <body>
            <div>Level 1</div>
            <div>Level 1</div>
       </body>
  2. Create a test like this:
        test('Repro', async ({ testUser }) => {
        await testUser.page.goto('file:///path/to/test.html');
        await testUser.page.getByText('Level 1', { exact: true }).click();

});
3. In VS Code, use "Pick locator."
4. Hover over one of the elements in the HTML page.
3. Repeat with variations like Level 1 1, Level 2000

Expected behavior

The suggested locators should use the full text provided to getByText in the test, but only the first word is included. Note the aka getByText('Level 1')

    Error: locator.click: Error: strict mode violation: getByText('Level 1', { exact: true }) resolved to 2 elements:
        1) <div>Level 1</div> aka getByText('Level 1').first()
        2) <div>Level 1</div> aka getByText('Level 1').nth(1)

Actual behavior

This suggested locator will only include the text before the last space in the string in all cases noted in the steps. Note the aka getByText('Level')

    Error: locator.click: Error: strict mode violation: getByText('Level 1', { exact: true }) resolved to 2 elements:
        1) <div>Level 1</div> aka getByText('Level').first()
        2) <div>Level 1</div> aka getByText('Level').nth(1)

When using pick locator it will also suggest the wrong text:

image

Additional context

No response

Environment

System:
    OS: macOS 14.5
    Memory: 172.08 MB / 16.00 GB
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.22 - ~/.yarn/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  Languages:
    Bash: 3.2.57 - /bin/bash

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions