ruby Selenium-Webdriver:找到元素后获取属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14288917/
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
Selenium-Webdriver: Get attributes after finding an element
提问by Jeff Sanders
I'm still rather new with the automation stuff, so this might sound like a stupid question. I did google search the hell out of it, before posting a question though :)
我对自动化的东西还很陌生,所以这听起来像是一个愚蠢的问题。在发布问题之前,我确实用谷歌搜索了它:)
Anyways, here is the problem
无论如何,这是问题所在
I am automating tests on an Android device One of the tests is to verify that an item has been marked as 'Favorite' Code snippet of page is:
我正在 Android 设备上进行自动化测试 其中一项测试是验证某个项目是否已标记为“收藏夹”页面的代码片段是:
<li class = "resultItem isFavorite" data-index="2">
<div class="name" data-cis="4ced6feb-3b5c-415a-ae1c-0b8bca8e3c85" onclick="return true">f,f</div>
</li>
I can find the element in the list with el = @driver.find_element(:xpath, "//*[class='name' and text() ='f,f']"). I was hoping that el.class would show me resultItem isFavorite.
Instead what i get is: Selenium:WebDriver::Element
我可以找到列表中的元素el = @driver.find_element(:xpath, "//*[class='name' and text() ='f,f']")。我希望 el.class 会告诉我 resultItem isFavorite。相反,我得到的是:Selenium:WebDriver::Element
If an item is not marked as favorite, the isFavorite flag isn't added in the field. I was hoping to use isFavorite to verify that an item is marked as favorite, but I can't seem to get it into a variable.
如果项目未标记为收藏夹,则不会在字段中添加 isFavorite 标志。我希望使用 isFavorite 来验证某个项目是否被标记为最喜欢的,但我似乎无法将其放入变量中。
Any help would be appreciated.
任何帮助,将不胜感激。
Thanks,
谢谢,
Jeff
杰夫
回答by JimEvans
What you really want here is something like the following:
您真正想要的是以下内容:
# Note: Possibly incorrect Ruby code here.
# Written from memory.
el = @driver.find_element(:xpath, "/your/xpath/here")
# Could also your el["class"]
element_class_attribute = el.attribute("class")
Ordinarily, using most attribute names like src(as in el.src) would give you a runtime error. However, it happens that classhas a special meaning in Ruby, and every object has a classattribute, which is the Ruby class.
通常,使用大多数属性名称src(如el.src)会给您带来运行时错误。然而,它恰好class在 Ruby中有一个特殊的含义,每个对象都有一个class属性,这就是 Ruby 类。
回答by Ben Amos
I'm reading two questions here. First, the short, easy one.
我在这里读两个问题。第一,简短的,容易的。
Q: Now that I've found el, how do I access its class HTML attribute?
问:既然我已经找到了el,我该如何访问它的类 HTML 属性?
A: Use el.attribute('class'), or its short-hand el['class']. See Selenium::WebDriver::Element.attribute.
A:使用el.attribute('class'),或者它的简写el['class']。请参阅Selenium::WebDriver::Element.attribute。
Now the longer one.
现在更长的一个。
Q: How can I test that the item denoted by elis marked as a favorite?
问:我如何测试由 表示的项目el是否被标记为收藏?
A: See below.
答:见下文。
To give a better answer, I would need more information about the composition of the page we're testing. I've made some basic assumptions:
为了给出更好的答案,我需要更多关于我们正在测试的页面组成的信息。我做了一些基本假设:
- The page has one or more
lielements with class'resultItem', some of which also have class'isFavorite'. - Each of these
lielements have one or more child elements, one of which is guaranteed to be adivwith class'name'. - Only one such child
divwill have text'f,f'.
- 页面有一个或多个
li带有 class 的元素'resultItem',其中一些也有 class'isFavorite'。 - 这些
li元素中的每一个都有一个或多个子元素,其中之一保证是divwith class'name'。 - 只有一个这样的孩子
div会有文本'f,f'。
Broadly speaking, we have two ways to go about this.
从广义上讲,我们有两种方法可以解决这个问题。
- We can find the relevant
div, then verify that its parentlihas class'isFavorite'. - We can find all
lielements with class'isFavorite', then verify that one of them contains the relevantdiv.
- 我们可以找到相关的
div,然后验证它的父li级是否有类'isFavorite'。 - 我们可以找到所有
li具有 class 的元素'isFavorite',然后验证其中一个包含相关的div.
There are a lot of things we need to be aware of here.
这里有很多事情我们需要注意。
For Selenium, XPath selectors are good at finding by text, but poor at finding by class. CSS selectors are good at finding by class, but cannot find by text. Neither of these is ideal for finding the divwe are after.
对于 Selenium,XPath 选择器擅长按文本查找,但不擅长按类查找。CSS 选择器擅长按类查找,但不能按文本查找。这些都不是找到div我们所追求的理想选择。
As mentioned above, we can get the class (attribute) of an element using element['class']. As we found out, element.classcalls Object#class, giving us Selenium::WebDriver::Element. el['class'], in this case, gives us the String 'name'. If called on its parent element, we would get 'resultItem isFavorite'.
如上所述,我们可以使用element['class']. 正如我们发现的那样,element.class调用Object#class,给了我们Selenium::WebDriver::Element。 el['class'],在这种情况下,给了我们 String 'name'。如果在其父元素上调用,我们将得到'resultItem isFavorite'.
We can get at an element's text using element.text. el.textwould give us 'f,f'in this case. Like JavaScript's textContent function, it returns the text of the element and all of its descendents, so calling text()on the parent element would also give us 'f,f'. Note that this method can be slow, and will return an empty String if the element is not visible in some way. Use with caution. See Selenium::WebDriver::Element.textfor more info.
我们可以使用element.text. 在这种情况下el.text会给我们'f,f'。与JavaScript 的 textContent 函数一样,它返回元素及其所有后代的文本,因此调用text()父元素也会为我们提供'f,f'. 请注意,此方法可能很慢,如果元素以某种方式不可见,它将返回一个空字符串。谨慎使用。有关更多信息,请参阅Selenium::WebDriver::Element.text。
Given the above, lets see how Methods #1 and #2 play out. (The following is Ruby 1.9.3.)
鉴于上述情况,让我们看看方法 #1 和 #2 如何发挥作用。(以下是 Ruby 1.9.3。)
Method #1
方法#1
div_elements = @driver.find_elements(:css, 'li > div.name').select { |e| e.text == 'f,f' }
div_element = div_elements.first
parent_li = div_element.find_element(:xpath, './..')
parent_li_classes = parent_li['class'].scan(/\S+/)
if parent_li_classes.include?('isFavorite')
# PASS
else
# FAIL
end
Method #2
方法#2
favorite_items = @driver.find_elements(:css, 'li.isFavorite')
if favorite_items.any? { |item| item.find_element(:css, 'div.name').text == 'f,f' }
# PASS
else
# FAIL
end
Method #2, Refactored
方法#2,重构
if @driver.find_elements(:css, 'li.isFavorite').any? { |item| item.text == 'f,f' }
# PASS
else
# FAIL
end
回答by Shubham Jain
Ruby
红宝石
element.attribute("attribute name")
Python
Python
element.get_attribute("attribute name")
Java
爪哇
element.getAttribute("attribute name")
C#
C#
element.GetAttribute("attribute name");
回答by Jeff Sanders
I was able to resolve this by going at it another way:
我能够通过另一种方式解决这个问题:
element = @driver.find_elements(:class, "isFavorite")
element.each do |t|
if t.text() == 'f,f' then
result = "pass"
end
end
I have a feeling this might be really slow, if there is a long list...but at least it works :)
我有一种感觉这可能真的很慢,如果有很长的列表......但至少它有效:)
Thanks
谢谢
Jeff
杰夫

