如何从 Selenium 获取元素的属性? [英] How to get attribute of element from Selenium?
本文介绍了如何从 Selenium 获取元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 Python 中使用 Selenium.我想获取 元素的
.val()
并检查它是否符合我的预期.
I'm working with Selenium in Python. I would like to get the .val()
of a <select>
element and check that it is what I expect.
这是我的代码:
def test_chart_renders_from_url(self):
url = 'http://localhost:8000/analyse/'
self.browser.get(url)
org = driver.find_element_by_id('org')
# Find the value of org?
我该怎么做?Selenium 文档似乎有很多关于选择元素的内容,但没有关于属性的内容.
How can I do this? The Selenium docs seem to have plenty about selecting elements but nothing about attributes.
推荐答案
您可能正在寻找 get_attribute()
.此处也显示了一个示例
You are probably looking for get_attribute()
. An example is shown here as well
def test_chart_renders_from_url(self):
url = 'http://localhost:8000/analyse/'
self.browser.get(url)
org = driver.find_element_by_id('org')
# Find the value of org?
val = org.get_attribute("attribute name")
这篇关于如何从 Selenium 获取元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文