Python 如何从Selenium获取元素的属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30324760/
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
How to get attribute of element from Selenium?
提问by Richard
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.
我正在 Python 中使用 Selenium。我想获得.val()
一个<select>
元素的 ,并检查它是否符合我的期望。
This is my code:
这是我的代码:
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?
How can I do this? The Selenium docs seem to have plenty about selecting elements but nothing about attributes.
我怎样才能做到这一点?Selenium 文档似乎有很多关于选择元素的内容,但没有关于属性的内容。
采纳答案by Saifur
You are probably looking for get_attribute()
. An example is shown hereas well
您可能正在寻找get_attribute()
. 一个例子示此处以及
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")
回答by Shubham Jain
Python
Python
element.get_attribute("attribute name")
Java
爪哇
element.getAttribute("attribute name")
Ruby
红宝石
element.attribute("attribute name")
C#
C#
element.GetAttribute("attribute name");
回答by DebanjanB
As the recent developed Web Applicationsare using JavaScript, jQuery, AngularJS, ReactJSetc there is a possibility that to retrieve an attribute of an element through Seleniumyou have to induce WebDriverWaitto synchronize the WebDriverinstance with the lagging Web Clienti.e. the Web Browserbefore trying to retrieve any of the attributes.
由于最近开发的Web 应用程序正在使用JavaScript、jQuery、AngularJS、ReactJS等,因此有可能通过Selenium检索元素的属性,您必须诱导WebDriverWait将WebDriver实例与滞后的Web 客户端同步,即之前的Web 浏览器试图检索任何属性。
Some examples:
一些例子:
Python:
To retrieve any attribute form a visibleelement (e.g.
<h1>
tag) you need to use the expected_conditionsasvisibility_of_element_located(locator)
as follows:attribute_value = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "org"))).get_attribute("attribute_name")
To retrieve any attribute form an interactiveelement (e.g.
<input>
tag) you need to use the expected_conditionsaselement_to_be_clickable(locator)
as follows:attribute_value = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "org"))).get_attribute("attribute_name")
Python:
检索任何属性形式的可视元素(例如
<h1>
标签),你需要使用expected_conditions为visibility_of_element_located(locator)
如下:attribute_value = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "org"))).get_attribute("attribute_name")
为了获取任何实体形式的互动元素(例如
<input>
标签),你需要使用expected_conditions为element_to_be_clickable(locator)
如下:attribute_value = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "org"))).get_attribute("attribute_name")
HTML Attributes
HTML 属性
Below is a list of some attributes often used in HTML
下面是一些 HTML 中常用的属性列表
Note: A complete list of all attributes for each HTML element, is listed in: HTML Attribute Reference
注意:每个 HTML 元素的所有属性的完整列表,列于:HTML 属性参考