如何在 Python Selenium 中获取 WebElement 的类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39688512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:35:37  来源:igfitidea点击:

How to get class name of the WebElement in Python Selenium?

pythonselenium-webdriver

提问by Alexander

I use Selenium webdriver to scrap a table taken from web page, written in JavaScript. I am iterating on a list of table rows. Each row may be of different class. I want to get the name of this class, so that I can choose appropriate action for each row.

我使用 Selenium webdriver 来删除从网页中获取的表格,用 JavaScript 编写。我正在迭代表行列表。每行可能属于不同的类别。我想获取这个类的名称,以便我可以为每一行选择适当的操作。

table_body=table.find_element_by_tag_name('tbody')
rows=table_body.find_elements_by_tag_name('tr')
for row in rows:
    if(row.GetClassName()=="date"):
        Action1()
    else:
        Action2()

Is this possible with Selenium? Or suggest another approach.

这可以用硒吗?或者建议另一种方法。

回答by alecxe

.get_attribute()methodis what you are looking for:

.get_attribute()方法就是你要找的:

row.get_attribute("class")