How to get tag name in Selenium Python?

Viewed 8951

Is there any way to get name of the tag of selenium web element??

I found, there is .getTagName() in selenium for java, here:https://www.tutorialspoint.com/how-do-i-get-a-parent-html-tag-with-selenium-webdriver-using-java

EDIT:

example: In this HTML, if I iterate though class='some_class_name', how can I get the tag_name (h2, p, ul or li)

<div class='some_class_name'>
    <h2>Some Random Heading 1</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry</p>

    <h2>Some Random Heading 2</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    <p>Random list are:</p>
    <ul>
        <li>Lorem</li>
        <li>Ipsum</li>
        <li>Dolor</li>
    </ul>
</div>

Python code may look like this..

content = driver.find_elements_by_xpath("//div[@class='some_class_name']//child::*")
for c in content:
    print(c.getTagName())      # something like this to get the tag of inner content..
1 Answers

Python you have tag_name to get the tag name.

content = driver.find_elements_by_xpath("//div[@class='some_class_name']//child::*")
for c in content:
     print(c.tag_name)
Related