如何使用selenium和css获取属性值 [英] how to get attribute value using selenium and css
问题描述
我有以下HTML代码:
I have the following HTML code:
<a href="/search/?p=2&q=move&mt=1"> 2 </a>
我想获得href中包含的内容,即,我正在寻找一个命令给我/ search /?p = 2& q = move& mt = 1的值。
I would like to get what is contained in href, ie, I was looking for a command which would give me "/search/?p=2&q=move&mt=1" value for href.
有人可以帮助我在selenium中的相应命令和css定位器,对于上述查询?
Could someone please help me with the respective command and css locator in selenium, for the above query?
如果我有类似的东西:
<a href="/search/?p=2&q=move&mt=1"> 2 </a>
<a href="/search/?p=2&q=move&mt=2"> 3 </a>
如果我要获取文本conatins'2'的href的属性值,那么我的css定位器synatx怎么样?
Out of these two if I was to get the attribute value for href whose text conatins '2', then how would my css locator synatx look like?
推荐答案
如果您的HTML完全由< a>
标签,则应该这样做:
If your HTML consists solely of that one <a>
tag, then this should do it:
String href = selenium.getAttribute("css=a@href");
您可以使用 DefaultSelenium#getAttribute()
方法,并传入CSS定位器, @
符号和要提取的属性的名称。在这种情况下,您选择 a
并获取其 @href
。
You use the DefaultSelenium#getAttribute()
method and pass in a CSS locator, an @
symbol, and the name of the attribute you want to fetch. In this case, you select the a
and get its @href
.
响应您的评论/编辑:
-
code>告诉Selenium该部分是属性的名称。
The part after
@
tells Selenium that that part is the name of the attribute.
)之前 @href
,因为它是定位器的一部分,而不是属性。所以,像这样:
You should place :contains('2')
before @href
because it's part of the locator, not the attribute. So, like this:
selenium.getAttribute("css=a:contains('2')@href");
这篇关于如何使用selenium和css获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!