如何在Selenium Python中获取标记名?

2024-09-30 20:28:53 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法获取selenium web元素标记的名称

我发现,seleniumforjava中有.getTagName(),这里:https://www.tutorialspoint.com/how-do-i-get-a-parent-html-tag-with-selenium-webdriver-using-java

编辑

示例:在这个HTML中,如果我遍历class='some_class='u 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..

Tags: name标记divgetseleniumrandomsomeli