如何从硒中获取元素的属性?

问题描述:

我正在用Python处理Selenium.我想获取<select>元素的.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")