如何从硒中获取元素的属性? [英] How to get attribute of element from Selenium?

查看:79
本文介绍了如何从硒中获取元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用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")

这篇关于如何从硒中获取元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆