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

Anu*_*paM 9 python selenium

有没有办法获取 Selenium Web 元素的标签名称?

我发现,.getTagName()在 Java 的 selenium 中,在如何使用 Java 使用 Selenium WebDriver 获取父 HTML 标签?

示例:在此 HTML 中,如果我迭代class='some_class_name',如何获取 tag_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>
Run Code Online (Sandbox Code Playgroud)

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...
Run Code Online (Sandbox Code Playgroud)

Kun*_*duK 17

在 Python 中,您必须tag_name获取标签名称。

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