Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to maintain a reference from a get_by_text Locator if the text changes? #20102

Closed
blksith0 opened this issue Jan 13, 2023 · 1 comment

Comments

@blksith0
Copy link

blksith0 commented Jan 13, 2023

thing = page.get_by_text('before')
print(thing.inner_text())
button.click() # changes the above thing's text
print(thing.inner_text()) # error

I want to search by text, at least initially, but still maintain a reference to that element after the text changes upon clicking the button.
What's the recommended way to do that?

@aslushnikov
Copy link
Collaborator

@blksith0 If you absolutely need to have a reference, then you'll have to come from locator to element handle:

thing = page.get_by_text('before').evaluate_handle(e => e)
print(thing.inner_text())
button.click() # changes the above thing's text
print(thing.inner_text()) # error

Do note though that holding a reference onto a DOM element has all kinds of associated issues:

  • the element might get detached
  • the element might get re-used by web framework to render completely different part of the page
  • there's no built-in auto-waiting logic like with locators that makes tests reliable

So in general, the rule of thumb is to avoid element handles and to try to find a good locator for the updated element.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants