You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Question] How to retrieve a selector text from a locator? #16807
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.tsimport{Page}from'@playwright/test';exportclassPlaywrightDevPage{readonlypage: Page;readonlymyButton: String;constructor(page: Page){this.page=page;this.myButton='#button-submit';}
...
asyncdoSomething(){awaitthis.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.
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.
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.
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....
Activity
thomaschaplin commentedon Aug 24, 2022
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.selectorand as a string by itself.Instead of having a way to "get" the selector, declare it as a variable and then use it throughout your code.
yury-s commentedon Aug 24, 2022
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.
VinzSpring commentedon Jun 5, 2023
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_allwhich requires the element's elector which I don't have.v-mwalk commentedon May 13, 2024
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....