playwrightplaywright-python

If I have a Playwright locator (using sync API and Python), can I determine the HTML element type it is of?


I'm using Sync API and Python for Playwright testing, and I have a python variable that is my locator, already found. I need to know what type of HTML element this locator is, because I need to do some extra coding for certain types of elements.

Say I did this in code: my_locator = self.page.get_by_role('some_valid_role')

Now I want to know if my_locator is a DIV element or a Span element? How can I do this?


Solution

  • The javascript property that indicates what the tag is tagName.

    Once you resolve the locator to an element you could evaluate the property, something like this:

    tagName = element.evaluate("el => el.tagName")