5

I'm using JSF 1.2. We want to write some selenium tests (based on xpath) but xpath does not seem to have wildcard matching on element id's.

We cannot turn off prefix ids as we are running as a portlet within IBM Portal Server 6.1 and our application breaks in that environment with the prefix turned off.

Currently we are using xpaths of the form

//*[substring(@id, 54)='id_distributorName']

which will match: <select size="1" class="firstName" name="viewns_7_8000CB1A0GUIE0IJF799CR10O2_:commonEntryForm:id_distributorName" id="viewns_7_8000CB1A0GUIE0IJF799CR10O2_:commonEntryForm:id_distributorName" >

but it strikes me that assuming JSF will always generate a viewId of the same length is dangerous.

Is there a better way to do this?

We've tried using the name attribute for our input controls but of course JSF ignores the attribute and writes it's own name attribute whose value matches the id presumably for evant handling scripting reasons)

4

3 回答 3

6

如果您使用的是 XPath 2.0,只需使用ends-with()函数:

//*[ends-with(@id, ':id_distributorName')]

如果您使用的是 XPath 1.0,请用于string-length()计算 的开头substring()

//*[substring(@id, string-length(@id) - 18) = ':id_distributorName']

这里,18 是id_distributorName(不带:前缀!)的长度。

于 2012-09-24T11:36:11.507 回答
5

好的,我刚刚回答了我自己的问题。

我承认我对 xpaths 知之甚少,这是我的借口!

答案是使用contains()

IE //*[contains(@id, 'id_distributorName')]

于 2012-09-24T10:48:20.583 回答
0

使用

//*[substring-after(@id, ':commonEntryForm:') = 'id_distributorName']
于 2012-09-24T13:08:02.990 回答