使用 selenium 获取下拉列表或多选列表中的项目顺序
使用 selenium 我想获取项目的顺序或者多选列表或下拉列表的键列表。 在 selenium API 中,我看到我们可以获取所选项目的索引,但我想获取所有项目的索引或键。是否可以?如果还那么怎么办? 或者,如果我可以获得列表中的项目索引和值,那也应该没问题。 或者无论如何,一次获取列表框/多选列表的所有键或值?
实际上我想验证给定的项目是否按正确的顺序排列。
(事实上,我想提供一个固定装置来验证给定的项目是否有序。 例如,列表框包含 A、B、C、D、E 有人用 (new String("A,B,c,D,E"),dropDownLocator) 调用此 API,那么它应该返回 true,否则返回 false。)
Using selenium I want to get the order of items or rather list of keys of the multiselect list or dropdown.
Seeing in selenium API i see that we can get the index of the selected items but I want to get the index or keys of all the items. Is it possible? If yet then how?
Or if i can get the list the items index and value that also should be fine.
Or anyway to get all the keys or values one at a time for the listbox/multiselect list?
Actually I want to verify that given items are in the correct order.
(Infact I want to provide a fixture to verify that given items are in order.
Say for example list box contains A,B,C,D,E
and some one calls this API with (new String("A,B,c,D,E"),dropDownLocator) then it should return true otherwise false.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@9ikhan 的答案对于获取属性是正确的,但我认为值得一提的是该答案的一个警告,并且如果实际上您正在寻找元素内容而不是属性值(如您所建议的那样),则还可以提供更直接的解决方案。
第 1 点:获取属性的语法。
定位器
"//some-xpath-here/@attribute-name"
是标准 XPath 的正确语法,但当说到属性 Selenium 不使用标准 XPath!相反,它使用了这个 - 请注意删除了最后的virgule:“//some-xpath-here@attribute-name”
(正如@Wesley Wiser在我的问题中最初向我指出的< a href="https://stackoverflow.com/questions/5171205/complications-with-seleniums-getattribute-method">Selenium 的 GetAttribute 方法的复杂性)。可能存在标准 XPath 语法可以工作的某些特殊情况——例如这个示例 :-)——但一般情况下请注意您需要使用 Selenium 语法。第 2 点:获取元素内容而不是属性。
这是我的相同代码版本。首先,我修改了 HTML,以清楚地描述内容中的属性值:
我的代码片段恰好是用 C# 编写的,但它实际上与前面的 Java 示例相同。请注意,我展示了两种变体——一种用于属性,另一种用于内容,因此您可以取消注释要测试的版本。
但如果您只想要内容,则存在更简单的解决方案:
The answer from @9ikhan is correct for getting attributes but I think it is worth mentioning one caveat with that answer, plus providing a more straightforward solution if, in fact, you are looking for the element content rather than attribute values, as you suggest.
Point 1: Syntax for getting an attribute.
The locator
"//some-xpath-here/@attribute-name"
is correct syntax for standard XPath but when it comes to attributes Selenium does not use standard XPath! Rather, it uses this--note the removal of the final virgule:"//some-xpath-here@attribute-name"
(as originally pointed out to me by @Wesley Wiser in my question Complications with Selenium's GetAttribute method). There may be certain peculiar instances where the standard XPath syntax will work -- such as this example :-) -- but in general be aware that you need to use the Selenium syntax.Point 2: Getting element content instead of attributes.
Here is my version of the same code. First my revised HTML to clearly delineate attribute values from content:
And my code fragment happens to be in C#, but it is virtually identical to the prior Java example. Note that I have shown two variations--one for attributes and one for content, so you can uncomment the one you want to test it.
But a much simpler solution exists if you just want content:
如果你的 html 是这样的
,那么你可以使用类似的东西
,我在这里使用了 java,你的选项元素将位于
optionList
中,你可以将其与现有列表进行比较。此外,这不适用于标签。说,无法从中检索 English(US),但此代码适用于任何属性。
if your html is like this
then you can use something like this
I've used java here, your option elements will be inside in the
optionList
, which you can compare against your existing list. Also, this won't work for labels. say<option value="en_US">English(U.S)</option>
,not able to retrieve English(U.S) from it, but this code will work for any attribute.