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

[Feature] ElementHandle.getTagName() #4845

Closed
tjenkinson opened this issue Dec 29, 2020 · 8 comments
Closed

[Feature] ElementHandle.getTagName() #4845

tjenkinson opened this issue Dec 29, 2020 · 8 comments

Comments

@tjenkinson
Copy link
Contributor

I'd like to retrieve the name of the tag from a playwright.ElementHandle, and it seems like this is not possible at the moment.

Something like

const $parent = await $el.$('xpath=..');
const tagName = await $parent.getTagName(); // <--
@JoelEinbinder
Copy link
Contributor

JoelEinbinder commented Dec 29, 2020

our ElementHandle extend JSHandle, so you can do

const tagName = await $parent.evaluate(e => e.tagName);
// or
const tagName = await $parent.getProperty('tagName');

@tjenkinson tjenkinson mentioned this issue Dec 29, 2020
@tjenkinson
Copy link
Contributor Author

@JoelEinbinder thanks. I tried$parent.evaluate but it looks like it runs in the main context not an isolated one? Does $parent.getProperty('tagName') run in its own context?

Thanks

@JoelEinbinder
Copy link
Contributor

All of these run in the main context. You expect the page to fight you on Element.tagName? That seems highly unusual.

Considering we have innerHTML and textContent, I don't see much harm in adding tagName. But I'm curious about why you would need it.

@tjenkinson
Copy link
Contributor Author

Do the calls like $el.getAttribute() also run on the main context or are these dev tools protocol? I'm still pretty fuzzy on how calls are getting through to the browser and where the work's happening.

I'm trying to trust the pages I'm running on as little as possible.

The particular thing I'm working on right now is trying to build up some bread crumbs from a target element up to the root, so that we can attempt to find it again later. For this I want to keep going up the tree and recording what the tag types are (along with other info like id if they have one and some of their innerText).

@tjenkinson
Copy link
Contributor Author

It looks like getAttribute() and the other functions you mentioned in "src/server/dom.ts" uses _evaluateInUtility

  async _evaluateInUtility<R, Arg>(pageFunction: js.Func1<[js.JSHandle<InjectedScript>, ElementHandle<T>, Arg], R>, arg: Arg): Promise<R> {
    const utility = await this._context.frame._utilityContext();
    return utility.evaluateInternal(pageFunction, [await utility.injectedScript(), this, arg]);
  }

Is this an isolated context?

I could probably submit a PR to add Element.tagName() if it would follow the same pattern?

@pavelfeldman
Copy link
Member

@tjenkinson: I'd stick with parent.evaluate(e => e.tagName) until it bites you? Otherwise we end up mirroring the entire DOM api. I think we were discussing the optional isolated world parameter in the evaluate with you. That's more likely to happen!

@tjenkinson
Copy link
Contributor Author

Yep that was me (#4377)

I will need the the call to be isolated. Happy to try and open a PR for #4377 if someone could point me in the right direction, otherwise I'll probably end up adding this in a fork because it looks simpler and using that for now.

@pavelfeldman
Copy link
Member

Folding into #4377

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

3 participants