How can I get the tag name in Selenium Python?
Question:
Is there a way to get the name of the tag of a Selenium web element?
I found, there is .getTagName()
in selenium for Java, in How do I get a parent HTML Tag with Selenium WebDriver using Java?.
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>
The 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...
Answers:
In 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)
Is there a way to get the name of the tag of a Selenium web element?
I found, there is .getTagName()
in selenium for Java, in How do I get a parent HTML Tag with Selenium WebDriver using Java?.
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>
The 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...
In 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)