如何在Selenium Python中获取标签名称?

时间:2020-10-12 20:07:34

标签: python selenium

有没有办法获取硒web元素标签的名称?

我发现,对于Java,硒中有.getTagName(),在此处:https://www.tutorialspoint.com/how-do-i-get-a-parent-html-tag-with-selenium-webdriver-using-java

编辑

示例:在此HTML中,如果我通过class ='some_class_name'进行迭代,那么如何获取标记名(h2pulli

<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代码可能看起来像这样。

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 个答案:

答案 0 :(得分:1)

Python,您有tag_name来获取标签名称。

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