Xpath Selenium Webdriver 2中是否支持字符串匹配()?

Xpath Selenium Webdriver 2中是否支持字符串匹配()?,xpath,selenium,selenium-webdriver,Xpath,Selenium,Selenium Webdriver,尊敬的Selenium Webdriver专家: 我想知道Selenium Webdriver中的string matches方法是否能与Java中的以下代码段正常工作: if (property.findElements(By.xpath("./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house']")).size() > 0 ) { // line 229 下面是第229行的xhtml网页:

尊敬的Selenium Webdriver专家:

我想知道Selenium Webdriver中的string matches方法是否能与Java中的以下代码段正常工作:

if (property.findElements(By.xpath("./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house']")).size() > 0 ) {    // line 229
下面是第229行的xhtml网页:

<dl class="cN-featDetails">
<dt class="proptype">Property type</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddPropertyType" class="propertytype type-house" title="Property type: House">House</dd>
我还尝试了
匹配(class,'propertytype.*$'])
但也没有成功

类的名称根据属性是房屋(类型房屋)还是公寓(类型公寓)而变化

关于如何在匹配项中使用正则表达式来检查此属性类型元素中是否存在值/有效的树节点,是否有任何建议

此代码段正在查找

我正在WindowsXP&7平台上使用Selenium 2.25.0、Java1.7.011

非常感谢您的建议。

不幸的是

WebDriver使用仅支持XPath 1.0的库

因此,您的XPath表达式是非法的,您应该将其重写为仅使用来自的特性和函数

我认为您可以简单地将示例中的
matches()
调用替换为。也就是说,通过
contains()匹配类名不是一个好的做法
,因为
类型房屋
也会匹配
类型房屋
。此外,如果匹配
属性类型房屋
,并且类的顺序恰好不同,它们将不会匹配。XPath不知道任何关于类的信息,也不知道CSS中使用的空格分隔列表。有关这方面的更多讨论,请参阅例如

您应该真正使用CSS选择器:

dl.cN-featDetails > dd.propertytype.type-house

属性类型house同样,在
matches()
调用中,您在
class
前面缺少了一个
@
。解决包含匹配过多问题的一种方法是使用
contains(concat(“”,@class,”),type house')
。是的。另外,使用
contains(@class,)可以解决不同顺序的类问题一级“)和包含(@class,“second-class”)
。理想情况下两者都用
concat()
链接,也可能是
normalize-space()
。感谢Slanec和Ross Patterson提供的详细答案。我使用if(property.findElements)(By.xpath(./dl[@class='cN-featDetails']]/dd[以(@class,'propertytype'))开头)。size()>0){因为它更简单,只需知道类名不会与-typeproperty相反即可工作。但是,我对学习CSS中的等效命令很感兴趣,但对它的工作原理却一知半解。再次感谢,Jack@JackBush
dl.cN-featDetails>dd.propertytype
几乎与f您的XPath就在那里。唯一的区别是它匹配具有
propertytype
类的
dd
元素。即使它们也有其他类,它也会工作。为了精确匹配您的XPath,我将编写
dl.cN-featDetails>dd[class^='propertytype']
dl.cN-featDetails > dd.propertytype.type-house