Ric*_*ard 61 python selenium webdriver getattribute selenium-webdriver
我在Python中使用Selenium.我想得到.val()一个<select>元素,并检查它是我期望的.
这是我的代码:
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?
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?Selenium文档似乎有很多关于选择元素但没有关于属性的内容.
Sai*_*fur 96
你可能正在寻找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")
Run Code Online (Sandbox Code Playgroud)
Shu*_*ain 31
蟒蛇
element.get_attribute("attribute name")
Run Code Online (Sandbox Code Playgroud)
Java的
element.getAttribute("attribute name")
Run Code Online (Sandbox Code Playgroud)
红宝石
element.attribute("attribute name")
Run Code Online (Sandbox Code Playgroud)
C#
element.GetAttribute("attribute name");
Run Code Online (Sandbox Code Playgroud)
由于最近开发的Web 应用程序使用JavaScript、jQuery、AngularJS、ReactJS等,因此有可能通过Selenium检索元素的属性,您必须诱导WebDriverWait将WebDriver实例与滞后的Web 客户端(即Web 浏览器)同步之前试图检索任何属性。
一些例子:
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")
Run Code Online (Sandbox Code Playgroud)为了获取任何实体形式的互动元素(例如<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")
Run Code Online (Sandbox Code Playgroud)下面是一些 HTML 中常用的属性列表
注意:每个 HTML 元素的所有属性的完整列表,列于:HTML 属性参考
| 归档时间: |
|
| 查看次数: |
100404 次 |
| 最近记录: |