Skip to content

[Question] How to retrieve a selector text from a locator? #16807

Closed

Description

Hi folks!

Thanks for the awesome framework (spoiler alert: my favorite one)!

I was wondering how to retrieve a selector text of the locator as a string.
For example:

const button = this.page.locator('#button-submit');
const selector = button.getSelector(); // => '#button-submit'

Thanks in advance!

Activity

  1. thomaschaplin commented on Aug 24, 2022

    @thomaschaplin
    Contributor

    You might benefit from using Page Object Model but maybe in a different way such as below... that way you can use the string in your page.selector and as a string by itself.

    // playwright-dev-page.ts
    import { Page } from '@playwright/test';
    
    export class PlaywrightDevPage {
      readonly page: Page;
      readonly myButton: String;
    
      constructor(page: Page) {
        this.page = page;
        this.myButton = '#button-submit';
      }
      
      ...
    
      async doSomething() {
        await this.page.selector(this.myButton).click();
        console.log(this.myButton) // this would result in "#button-submit"
      }
      
      ...
    }

    Instead of having a way to "get" the selector, declare it as a variable and then use it throughout your code.

  2. yury-s commented on Aug 24, 2022

    @yury-s
    Member

    As suggested above there is no API to get selector from a locator. If you want to reuse a selector it can be saved to a variable and later used to construct multiple locators.

    Relevant feature request: #12553

    Closing this issue as we're not planning to expose the selector at the moment.

  3. VinzSpring commented on Jun 5, 2023

    @VinzSpring

    It would be really nice to get the selector from a locator.
    I have a locator where I want to check wether its children have "aria-expanded" set to true. For this I am using eval_on_selector_all which requires the element's elector which I don't have.

  4. v-mwalk commented on May 13, 2024

    @v-mwalk

    Having an optional string property (or even a general-use object property!) in a Locator would be really nice. When a Locator fails for any reason (element doesnt exist, etc), within a framework I present a 'human-readable' error (So, instead of 'Error: some-non-human-readable-selector not found' I have 'Error: Plan [Energy: Gas, Name: Flexible] not showing'. At the moment, I have to wrap a Locator in an object with the Playwright locator, original selector string and human-readable description. Would be easy to add to a Locator with no impact, but would enable 'advanced' framework designers ability to really enrich Locators....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions